Search code examples
androidbitmapandroid-storage

Cant save image to gallery


I am trying to save bitmap as an image to the phone storage with MediaStore like this:

 MediaStore.Images.Media.insertImage(contentResolver, bitmap, "title" , "description")

The image was not saved inside the device storage, I have checked on both emulator and physical devices.


What I did so far:

  • Checked that the bitmap that I am passing is my wanted bitmap:

    For this, I have checked its value with the help of the debugger like this:

    1) Put a breakpoint on the line that supposed to save the image and check what bitmap I am passing, the bitmap was the same bitmap that I wanted to save:

    enter image description here

    2) This is how the bitmap looks like:

enter image description here

  • Made sure that the permission for writing files to the external storage (the line below) was granted from the user

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

  • Made sure that the image was not saved at the end of the gallery or in any other place inside the device storage


I know that I can probably save the image without using MediaStore as mentioned in this thread but I want to know what I am doing wrong here and why I can't save the bitmap as an image inside the device storage.


Solution

  • For some reason even after WRITE_EXTERNAL_STORAGE was granted, after looking a bit on the logs I have noticed that I am getting this error:

    Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=11218, uid=10079 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
    

    It looked weird to me because I have checked that this permission got granted before saving the image to the gallery.

    What solved it for me was to delete the app and reinstall it again