I'm trying to display an image uploaded from the gallery in an AlertDialog but it's not working for me right now.
My code:
private void dispatchTakePictureIntent() {
Intent imageGetter = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
activityLauncher.launch(imageGetter);
}
ActivityResultLauncher<Intent> activityLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> {
Uri selectedImage = result.getData().getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string
Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String user_image_path = cursor.getString(columnIndex);
myImageView = new ImageView(getContext());
Picasso.get().load(user_image_path).fit().centerCrop().into(myImageView);
AlertDialog.Builder builder =
new AlertDialog.Builder(getContext()).
setMessage("Message above the image").
setPositiveButton("OK", (dialog, which) -> dialog.dismiss()).
setView(myImageView);
builder.create().show();
cursor.close();
});
The gallery opens up and I can choose a picture but after I choose a picture it goes back and a blank dialog with no image (the message is there) shows up.
I tried adding "file:/// " to the Picasso but that doesn't work.
Thanks.
Why not use the Uri directly to display the picture? Just like:
Uri selectedImage = result.getData().getData();
Picasso.get().load(selectedImage).fit().centerCrop().into(myImageView);
At the same time, it is worth noting that
resolver.query()
is a I/O operation. It is not recommended to operate directly on the main thread
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.