BitmapFactory: Unable to decode stream: java.io.FileNotFoundException:open failed: EACCES (Permission denied) on Android Q
Adding android:requestLegacyExternalStorage="true"
to the manifest's application block could actually solve the issue!
On Android 10, they introduced the concept of "Scoped Storage" and this way, you no longer can OPEN a image using its path. You can get more info HERE.
So now you have to decode it using its ParcelFileDescriptor
and its Uri
.
You can:
final Cursor cursor = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if (cursor.moveToFirst()) {
final ImageView imageView = (ImageView) findViewById(R.id.pictureView);
if (Build.VERSION.SDK_INT >= 29) {
// You can replace '0' by 'cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)'
// Note that now, you read the column '_ID' and not the column 'DATA'
Uri imageUri= ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getInt(0));
// now that you have the media URI, you can decode it to a bitmap
try (ParcelFileDescriptor pfd = this.getContentResolver().openFileDescriptor(mediaUri, "r")) {
if (pfd != null) {
bitmap = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
}
} catch (IOException ex) {
}
} else {
// Repeat the code you already are using
}
}