0

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.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • What does `insertImage` return? Do you have any logs in logcat with `MediaStore` tag? – esentsov Sep 27 '19 at 15:07
  • @esentsov I didn't find any logs using `MediaStore` , but nice idea. And I have just checked what does `insertImage` do and I found this *@return The URL to the newly created image, or null if the image failed to be stored * for any reason.* – Tamir Abutbul Sep 27 '19 at 15:11
  • I meant what it returns in your code. Does it return null? – esentsov Sep 27 '19 at 15:13
  • 1
    @esentsov thank you for your help. I managed to solve this and will write answer if you want to check it later – Tamir Abutbul Sep 27 '19 at 15:48

2 Answers2

0

I tried this but not worked for me too. So I used Android Download Manager, which is simple and easy ;-)

Here is the code

/*
This method can be used to download an image from the internet using a url in Android. This use Android Download Manager to
download the file and added it to the Gallery. Downloaded image will be saved to "Pictures"
Folder in your internal storage
*/

private void downloadImageNew(String filename, String downloadUrlOfImage){
    try{
        DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Uri downloadUri = Uri.parse(downloadUrlOfImage);
        DownloadManager.Request request = new DownloadManager.Request(downloadUri);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false)
                .setTitle(filename)
                .setMimeType("image/jpeg") // Your file type. You can use this code to download other file types also.
                .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                .setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,File.separator + filename + ".jpg");
        dm.enqueue(request);
        Toast.makeText(this, "Image download started.", Toast.LENGTH_SHORT).show();
    }catch (Exception e){
        Toast.makeText(this, "Image download failed.", Toast.LENGTH_SHORT).show();
    }
}
0

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

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53