Why Images.Media.insertImage return null

大城市里の小女人 提交于 2019-11-26 18:32:06

问题


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.


回答1:


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.




回答2:


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.




回答3:


I ran into this issue when I was running tests on an emulator with a fresh sdcard image. I was able to solve the problem by creating /sdcard/Pictures with

new File("/sdcard/Pictures").mkdirs();




回答4:


When i had such a problem i fixed it with adding permissions:

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



回答5:


I was executing MediaStore.Images.Media.insertImage before getting user permission to WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE which was causing a null path

So make sure user has allowed for permission and then store image




回答6:


Make sure you have added permissions in your manifest file.

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

Then add this code before calling

MediaStore.Images.Media.insertImage

File sdcard = Environment.getExternalStorageDirectory();
    if (sdcard != null) {
        File mediaDir = new File(sdcard, "DCIM/Camera");
        if (!mediaDir.exists()) {
            mediaDir.mkdirs();
        }
    }


来源:https://stackoverflow.com/questions/12230942/why-images-media-insertimage-return-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!