我是Android
的初学者。
我将从相机和文件浏览器制作图像选择器。
我发现了谷歌搜索的示例代码。
但捕获后,它不会调用onActivityResult()
方法。
所以我无法处理捕获结果。
注意:我正在处理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);
}
答案 0 :(得分:1)
调用getActivity().startActivityForResult()
会在托管片段的活动上调用 onActivityResult(),而不是 onActivityResult() 片段自我。
如果您想在片段本身中调用 onActivityResult()
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
如果你想处理它,你需要调用的活动:
getActivity().startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
此外,您可能希望将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);
}
答案 1 :(得分:0)
在您的片段代码中,您需要从片段中调用活动。而不是:
getActivity().startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
您需要调用以下内容,因为您的代码位于嵌套类中。
YOUR_FRAGMENT.this.startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
没有getActivity
。