[英]cannot access onActivityResult() method in fragment
I am a beginner in Android
. 我是
Android
的初学者。
I am going to make image picker from camera and file explorer. 我将从相机和文件资源管理器制作图像选择器。
I found out example code from google search. 我从Google搜索中找到了示例代码。
But after capture, it don't call onActivityResult()
Method. 但是捕获后,它不会调用
onActivityResult()
方法。
So I cannot handle the capture result. 因此我无法处理捕获结果。
note: I am working on Fragment
. 注意:我正在研究
Fragment
。
public void selectImage(){
final String [] items = new String [] {"Take Photo", "Choose Photo"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(), android.R.layout.select_dialog_item, items);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add/Change Photo");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mImageCaptureUri = Uri.fromFile(file);
try {
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
}
catch (Exception e) {
e.printStackTrace();
}
dialog.cancel();
}
else {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getActivity().startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
} );
final AlertDialog dialog = builder.create();
dialog.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode,data);
// TODO Fix no activity available
if (resultCode != getActivity().RESULT_OK)
return;
Bitmap bitmap = null;
String path = "";
if (requestCode == PICK_FROM_FILE) {
mImageCaptureUri = data.getData();
path = getRealPathFromURI(mImageCaptureUri); // From Gallery
if (path == null)
path = mImageCaptureUri.getPath(); // From File Manager
if (path != null)
bitmap = BitmapFactory.decodeFile(path);
}
else {
path = mImageCaptureUri.getPath();
bitmap = BitmapFactory.decodeFile(path);
}
imgProfile.setImageBitmap(bitmap);
super.onActivityResult(requestCode, resultCode, data);
}
calling getActivity().startActivityForResult()
will result a call to onActivityResult() on the Activity hosting the Fragment and not the onActivityResult() of the Fragment it self. 调用
getActivity().startActivityForResult()
将导致在托管片段的Activity上调用onActivityResult() ,而不是其自身的Fragment的onActivityResult() 。
If you want a call to onActivityResult() in the Fragment itself call 如果要在Fragment本身调用中调用onActivityResult()
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
and if you want to handle it it the Activity you need to call: 如果要处理它,则需要调用Activity:
getActivity().startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
Also you might want to reorganize your onActivityResult() as 另外,您可能希望将onActivityResult()重组为
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// TODO Fix no activity available
if (resultCode != getActivity().RESULT_OK)
Bitmap bitmap = null;
String path = "";
if (requestCode == PICK_FROM_FILE) {
mImageCaptureUri = data.getData();
path = getRealPathFromURI(mImageCaptureUri); // From Gallery
if (path == null)
path = mImageCaptureUri.getPath(); // From File Manager
if (path != null)
bitmap = BitmapFactory.decodeFile(path);
} else {
path = mImageCaptureUri.getPath();
bitmap = BitmapFactory.decodeFile(path);
}
imgProfile.setImageBitmap(bitmap);
}
In your fragment code you need to call the activity from the fragment. 在您的片段代码中,您需要从片段中调用活动。 Instead of:
代替:
getActivity().startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
you need to call the following, as your code is inside a nested class. 您需要调用以下代码,因为您的代码位于嵌套类中。
YOUR_FRAGMENT.this.startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
without getActivity
. 没有
getActivity
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.