androidpermission-deniedbitmapfactory

BitmapFactory.decodeFile e/bitmapfactory unable to decode stream java.io.filenotfoundexception EACCES (Permission denied)


I tried to take a picture from storage, and display it in ImageView using BitmapFactory.decodeFile but i found error unable to decode stream java.io.filenotfoundexception EACCES (Permission denied), i'm using android 10 emulator API 30.

this is my code :

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK)
        {
            if(requestCode == REQUEST_GALLERY)
            {
                Uri dataimage = data.getData();
                String[] imageprojection = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(dataimage,imageprojection,null,null,null);

                if (cursor != null)
                {
                    cursor.moveToFirst();
                    int indexImage = cursor.getColumnIndex(imageprojection[0]);
                    part_image = cursor.getString(indexImage);

                    Toast.makeText(this, "Image Saved Part Image :" + part_image, Toast.LENGTH_SHORT).show();

                    if(part_image != null)
                    {
                        File image = new File(part_image);
                        imgHolder.setImageBitmap(BitmapFactory.decodeFile(image.getAbsolutePath()));
                        Toast.makeText(this, "Image Saved Part Image :" + image, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    }
    

I have added this to my Android manifest :

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

and this :

<application
...
android:requestLegacyExternalStorage="true">

This is my error log :

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/0000-0000/DCIM/Camera/IMG-20200714-WA0008.jpg: open failed: EACCES (Permission denied)

any help solution ?


Solution

  • imgHolder.setImageUri(data.getData());