Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Images.Media.insertImage return null

Tags:

I have some code where I run the method MediaStore.Images.Media.insertImage (inserting it from a source not a file name), This code saves the image to the MediaStore and returns the uri of the image. I know that when it fails for any reason it will return null instead of a uri. This image has been downloaded multiple times by many people and every once in a while it will return null from this method. I have never had this happen to me so I have no idea what is going on. What are reasons why this could happen? There is another post with the same issue but the answer is a link to the source code for MediaStore but that link goes to a page saying the link is unavailable. Any help would be appreciated. Thanks.

After removing my SD card I got this error so I know that could be a reason, I'm not sure but I feel that it would also happen if the card was full. Still just wondering if there could be another reason too.

like image 491
dj.lnxss Avatar asked Sep 01 '12 19:09

dj.lnxss


2 Answers

It seems to happen when you don't have an /sdcard/DCIM/Camera directory on some versions of Android. Creating this directory (and having the permission) solved the problem for me.

like image 195
thomasd Avatar answered Sep 25 '22 10:09

thomasd


MediaStore.Images.Media.insertImage is actually accessing external storage to save the image.

Some important reminders which might be causing your app to fail:

  1. A USB connection will block SD card usage if in Mass Storage mode.

  2. There may be other factors that might lead to an SD card being inaccessible, so make sure that you can access the SD card using a file browser first.

  3. Make sure that your permissions are correctly configured with android.permission.WRITE_EXTERNAL_STORAGE

Posting this here for completeness, as I was getting a null from insertImage, and the cause was 1.

like image 25
00500005 Avatar answered Sep 25 '22 10:09

00500005