MediaStore.Images.Media.insertImage is returning null when trying to save the image

后端 未结 2 584
心在旅途
心在旅途 2020-12-14 09:22

I am using an custom view and in that i am using an canvas in which a user can draw anything and after that i want to save that image in sd card bt was not able to do that.

2条回答
  •  无人及你
    2020-12-14 09:58

    I had this issue in the Emulator (Android 4.4) and turns out it's due to an Android bug, where it happens when the user hasn't taken a photo on the device before (i.e. gallery is empty and hasn't been initialized.). The workaround is to initialize the photo directory manually:

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

    Not sure if this is fixed in later versions of Android.

提交回复
热议问题