[Android] Image related - absolute path obtained, but it doesn't get written to imgView...

Asked 1 years ago, Updated 1 years ago, 92 views

Hello, I'm a beginner studying Android.

When you click a specific button, the gallery screen is displayed through "ACTION_PICK" I tried to display the selected image in the desired image view by selecting the image.

Originally Bitmap image_bitmap = MediaStore.Images.Media .getBitmap(getContentResolver(), data.getData());

(Bitmap value received with this) content://media/external/images/media/10082

When you draw an image with this, the selected image is well drawn in the image view.

(The reason why I want to find an absolute path and put it on the image view is because I want to save the absolute path of the image as a String and then float the image as a saved path when I turn on the app again. I tried to save the context path above this and draw an image in a list view, but it failed.)

So I tried to find the absolute path of the image and draw it (Type: Galaxy s6 - Image stored in internal gallery)

Uri selectedImg_uri;

this.selectedImg_uri = Uri.parse(selectedImg_path);

The current selectedImg_uri value is /storage/emulated/0/Download/13659028_1035067996585531_202260389946916935_n.jpg.

Attempt Method 1)

//Drawing

image_selected.setImageURI(selectedImg_uri);

Attempt Method 2)

File img_pathFile = new File(String.valueOf(selectedImg_uri));

Bitmap bm_imgPath;

BitmapFactory.Options bm_options;

try{    
    bm_imgPath=BitmapFactory.decodeFile(img_pathFile.getAbsolutePath());    
    System.out.println("Try(aaaa)");    
} catch (OutOfMemoryError e){
    bm_options = new BitmapFactory.Options();
    bm_options.inSampleSize = 2;
    bm_imgPath = BitmapFactory.decodeFile(img_pathFile.getAbsolutePath(), bm_options);
    System.out.println("OutOfMemoryError(aaaa)");
} catch (Exception e){
    bm_imgPath = null;
    System.out.println("Exception(aaaa)");
}

System.out.println ("This is...?(aaaa):"+String.valueOf(bm_imgPath))); (→Yes, this is null).);

System.out.println("bm_imgPath(aaaa):"+String.valueOf(img_pathFile.getAbsolutePath()));

//Drawing image_selected.setImageBitmap(bm_imgPath);

android image absolute-path

2022-09-22 21:16

1 Answers

In my environment, I checked that it comes out well with method 1. Check to see if the file exists or if there is no problem with the other code.

This code is set to imageView using the absolute path of the image I tested.

String path = "/storage/emulated/0/Download/PS16011800314.jpg"
imageView.setImageURI(Uri.parse(path));

If the OS is marshmallow, please make sure that the permission for READ_EXTERNAL_STORAGE has been granted. Please check the following link for permission processing.


2022-09-22 21:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.