Glide Version: 4.9.0
Integration libraries:
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)
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
int i = 0;
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.
Most helpful comment
check if this permissions exists in your manifest :
use the code below to get all images from gallery :
``` getAllImages(Cursor cursor, Activity activity) { arrayList = new ArrayList<>();
public static ArrayList
int i = 0;
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();
}
public class Photo {
private String name;
private Uri uri;
}
```
To avoid all of this , make the requestLegacyExternalStorage true
android:requestLegacyExternalStorage="true"