Java >> Java tutorial >  >> Tag >> java.io

BitmapFactory:Kan ikke afkode stream:java.io.FileNotFoundException:open failed:EACCES (Permission denied) på Android Q

Tilføjer android:requestLegacyExternalStorage="true" til manifestet 's programblok kunne faktisk løse problemet!


På Android 10 introducerede de konceptet "Scoped Storage", og på denne måde kan du ikke længere ÅBNE et billede ved at bruge dets vej. Du kan få mere info HER.

Så nu skal du afkode den ved hjælp af dens ParcelFileDescriptor og dens Uri .

Du kan:

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
    }
}

No
Java tag