我想把里onActivityResult中文件的地址传出来给按钮点击响应里面的文件上传用但是发现不行,求大神解答一下
Btu_shangchuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");//设置类型
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 1);
String path = intent.getStringExtra("path");
final BmobFile bmobFile = new BmobFile(new File(path));
bmobFile.uploadblock(ZuoYeShangJiao.this, new UploadFileListener() {
@Override
public void onSuccess() {
MyUser user = BmobUser.getCurrentUser(ZuoYeShangJiao.this, MyUser.class);
Homework homework = new Homework();
homework.setStu_Number(user.getUsername());
homework.setHw_class(user.getStu_class());
homework.setHw_course(course);
homework.setHomework(bmobFile);
homework.save(ZuoYeShangJiao.this, new SaveListener() {
@Override
public void onSuccess() {
Toast.makeText(ZuoYeShangJiao.this, "作业上传成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int i, String s) {
Toast.makeText(ZuoYeShangJiao.this,"作业上传失败",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onFailure(int i, String s) {
Toast.makeText(ZuoYeShangJiao.this,"上传失败",Toast.LENGTH_SHORT).show();
}
});
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
/*String[] proj = {MediaStore.Images.Media.DATA};
Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
File file = new File(img_path);*/
String path = uri.toString();
data.putExtra("path",path);
}
super.onActivityResult(requestCode, resultCode, data);
}
注:Bmob类是一个云数据库的uploadbock是当中的一个文件上传方法
1.在当前类创建一个全局变量:String path,在onActivityResult()方法中把获取的path赋值给全局变量,然后再onClick()方法中获取全局变量的path值,谢谢可以帮助到你。。。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。