Search code examples
javaandroidbitmapbitmapfactory

Permission denied when creating bitmap from photo path


I am trying to create bitmap from photo path like this:

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);

And I have added permissions in manifest:

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

It works fine before. But suddenly it got a problem, says:

    E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/image_1.jpg: open failed: EACCES (Permission denied)

I have been searching the answer for a few days, but still can not figure it out. I am totally new to android and this is my first post. Hope you can help me with it. Thanks!

For your reference, I am using android studio and genymotion. The app API is 23.


Solution

  • The app has not been approve for the permission to write to the external memory, this is a new feature for android 23 Marshmallow called Run time permission.

    You first need to create a runtime permission for user to accept and approve to write to the external memory before you can write to it.