I want to pick a picture from gallery. I have an implicit intent and onActivityResult but it is never called. I have looked for possible solutions like this:
setResult(RESULT_LOAD_IMG, galleryIntent);
Here is my code in the 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.
RESULT_LOAD_IMG = 1
it might be possible if 1
is set in base activity onaActivitResult then AddQuestionActivity
onactivityResult won't go forward
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);
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.