Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image File From Local Storage to ImageView - android 10 Storage Permissions Issue

Tags:

Not sure where I am making an error, but I have tried everything to get an image to display in ImageView once the user selects an Image in Image Picker but let's leave the image picker aside I can't even show the image by directly providing the FullPath as in code below (not getting any error either)

Try 1 - Bitmap

string IMGpath = "/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"
var imgFile = new File(IMGpath);
if (imgFile.Exists())
 {       
  Bitmap bitmap = BitmapFactory.DecodeFile(imgFile.AbsolutePath);     
  _imageview.SetImageBitmap(bitmap);
  }
else
  {
    Log.Info("AAABL", "No file");
  } 

Try 2 - Bitmap with options

string IMGpath = "/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"
    var imgFile = new File(IMGpath);
    if (imgFile.Exists())
     { 
      BitmapFactory.Options bmOptions = new BitmapFactory.Options();
      Bitmap bitmap = BitmapFactory.DecodeFile(imgFile.AbsolutePath, bmOptions);
      bitmap = Bitmap.CreateScaledBitmap(bitmap, 200,200, true);
      _imageview.SetImageBitmap(bitmap);
      }
    else
      {
        Log.Info("AAABL", "No file");
      } 

Try 3 - Custom Library In addition, I tried to use a Library as follows

string IMGpath = "/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"
        var imgFile = new File(IMGpath);
        if (imgFile.Exists())
         { 
           var a = ImageService.Instance.LoadFile(imgFile.AbsolutePath)
                            .Retry(3, 200)
                            .Into(_imageview);
          }
        else
          {
            Log.Info("AAABL", "No file");
          } 

Try 4 - URI

string IMGpath = "/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"
Android.Net.Uri URI = Android.Net.Uri.Parse(IMGpath );
_imageview.SetImageURI(URI ); 

No matter what I do, I cannot get the image to load. I have the following permissions in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

And also checking Permissions on Activity OnCreate Method to ensure the app has full permissions to storage as following

if ((ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted) && (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted))
   {
     ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }, 0);
   }

This only happens in Android 10 (API 29) where same code works fine up to API 28.

This is one heck of a soul crusher, whole day and I couldn't figure it out, any idea?

UPDATE:: When tried to open the file I get the following error

open failed: EACCES (Permission denied)

like image 729
Ali Avatar asked Dec 03 '19 07:12

Ali


1 Answers

I ran into the same issue and found your post, then afterwards found a potential solution for you here: https://github.com/bumptech/glide/issues/3896#issuecomment-533062076

Just add this to your manifest's application tag. It fixed the issue for me on 10:

android:requestLegacyExternalStorage="true"

like image 77
bo malone Avatar answered Oct 12 '22 21:10

bo malone