[英]onActivityResult is never called
I want to pick a picture from gallery. 我想从画廊挑选一张照片。 I have an implicit intent and onActivityResult but it is never called.
我有一个隐式的intent和onActivityResult,但从未调用过它。 I have looked for possible solutions like this:
我一直在寻找这样的可能的解决方案:
setResult(RESULT_LOAD_IMG, galleryIntent);
Here is my code in the AddQuestionActivity: 这是我在AddQuestionActivity中的代码:
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
public static Intent galleryIntent;
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Create intent to Open Image applications like Gallery, Google Photos
galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
// Set the Image in ImageView after decoding the String
imageView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
Thanks for your advice. 谢谢你的建议。
please check your base activity onActivityResult
if you have. 如果有,请在
onActivityResult
检查您的基本活动。
RESULT_LOAD_IMG = 1 RESULT_LOAD_IMG = 1
it might be possible if 1
is set in base activity onaActivitResult then AddQuestionActivity
onactivityResult won't go forward 如果基本活动onaActivitResult中设置为
1
,则AddQuestionActivity
onactivityResult将不会继续进行
Update your code: 更新您的代码:
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.