Glide: Can't display local images due to Android Q update

Created on 20 Sep 2019  路  7Comments  路  Source: bumptech/glide

Glide Version: 4.9.0

Integration libraries:

  • OkHttp3

Device/Android Version: Google pixel 2/ Android 10

Issue details / Repro steps / Use case background:

Stack trace / LogCat:
I/Glide: Root cause (2 of 3)
java.io.FileNotFoundException: open failed: EACCES (Permission denied)
at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:315)
at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:220)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1498)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1420)
at com.bumptech.glide.load.data.FileDescriptorLocalUriFetcher.loadResource(FileDescriptorLocalUriFetcher.java:22)
at com.bumptech.glide.load.data.FileDescriptorLocalUriFetcher.loadResource(FileDescriptorLocalUriFetcher.java:14)
at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44)

stale

Most helpful comment

check if this permissions exists in your manifest :

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

use the code below to get all images from gallery :

```
public static ArrayList getAllImages(Cursor cursor, Activity activity) {
int i = 0;
ArrayList arrayList = new ArrayList<>();
if (cursor == null) {
ContentResolver resolver = activity.getContentResolver();
cursor = resolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
null);
if (cursor != null) {
while (i < cursor.getCount()) {
cursor.moveToPosition(i);
int fieldIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
Long id = cursor.getLong(fieldIndex);
Uri imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
Photo photo = new Photo();
photo.setName("");
photo.setUri(imageUri);
arrayList.add(photo);
i++;
}
cursor.close();
}

    }
    return arrayList;
}

public class Photo {
private String name;
private Uri uri;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Uri getUri() {
    return uri;
}

public Photo(String name, Uri uri) {
    this.name = name;
    this.uri = uri;
}

public Photo() {
}

public void setUri(Uri uri) {
    this.uri = uri;
}

}
```

To avoid all of this , make the requestLegacyExternalStorage true

android:requestLegacyExternalStorage="true"

All 7 comments

check if this permissions exists in your manifest :

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

use the code below to get all images from gallery :

```
public static ArrayList getAllImages(Cursor cursor, Activity activity) {
int i = 0;
ArrayList arrayList = new ArrayList<>();
if (cursor == null) {
ContentResolver resolver = activity.getContentResolver();
cursor = resolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
null);
if (cursor != null) {
while (i < cursor.getCount()) {
cursor.moveToPosition(i);
int fieldIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
Long id = cursor.getLong(fieldIndex);
Uri imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
Photo photo = new Photo();
photo.setName("");
photo.setUri(imageUri);
arrayList.add(photo);
i++;
}
cursor.close();
}

    }
    return arrayList;
}

public class Photo {
private String name;
private Uri uri;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Uri getUri() {
    return uri;
}

public Photo(String name, Uri uri) {
    this.name = name;
    this.uri = uri;
}

public Photo() {
}

public void setUri(Uri uri) {
    this.uri = uri;
}

}
```

To avoid all of this , make the requestLegacyExternalStorage true

android:requestLegacyExternalStorage="true"

check if this permissions exists in your manifest :

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

use the code below to get all images from gallery :

public static ArrayList<Photo> getAllImages(Cursor cursor, Activity activity) {
       int i = 0;
       ArrayList<Photo> arrayList = new ArrayList<>();
       if (cursor == null) {
           ContentResolver resolver = activity.getContentResolver();
           cursor = resolver.query(
                   MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                   null,
                   null,
                   null,
                   null);
           if (cursor != null) {
               while (i < cursor.getCount()) {
                   cursor.moveToPosition(i);
                   int fieldIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
                   Long id = cursor.getLong(fieldIndex);
                   Uri imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
                   Photo photo = new Photo();
                   photo.setName("");
                   photo.setUri(imageUri);
                   arrayList.add(photo);
                   i++;
               }
               cursor.close();
           }

       }
       return arrayList;
   }
public class Photo {
    private String name;
    private Uri uri;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Uri getUri() {
        return uri;
    }

    public Photo(String name, Uri uri) {
        this.name = name;
        this.uri = uri;
    }

    public Photo() {
    }

    public void setUri(Uri uri) {
        this.uri = uri;
    }

}

To avoid all of this , make the requestLegacyExternalStorage true

android:requestLegacyExternalStorage="true"

Thanks, now it works.
But how about if I want to get all media files(Both Image and Video)?
I tried to user MediaStore.Files.getContentUri("external") but does not work

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.

This is solve by passing Uri instead of string in load method of glide, just like this

              GlideApp.with(imageView.context)
                    .load(Uri.parse("file path in string"))
                    .into(imageView)

This is solve by passing Uri instead of string in load method of glide, just like this

              GlideApp.with(imageView.context)
                    .load(Uri.parse("file path in string"))
                    .into(imageView)

Does no work tested on android R.
the method which @Mouadabdelghafouraitali suggested works like charm.

Make file instance and pass file's URI that you want to load into ImageView You can also use RequestListener to trace the error in case of failure

Glide.with(context)
        .load(new File(fileUri.getPath())) // Uri of the picture
        .into(imageview)
        .listener(new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            Log.e("xmx1","Error "+e.toString());
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            Log.e("xmx1","no Error ");
            return false;
        }
    });

inside your application tag
android:requestLegacyExternalStorage="true"

It's working fine for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kenneth2008 picture kenneth2008  路  3Comments

MrFuFuFu picture MrFuFuFu  路  3Comments

Ncit picture Ncit  路  3Comments

Tryking picture Tryking  路  3Comments

sant527 picture sant527  路  3Comments