java.lang.IllegalStateException: Failed to build unique file: /storage/emulated/0/Pictures Title image/jpeg Android 10(Samsung note 10+)

I am using the below code to get image uri from camera

public static Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

Solution 1:

This came as an error only in android 10,earlier versions was working fine with this code.Any ways irrespective of the version I just changed the hardcoded "Title" in insertImage() to

public static Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, **"IMG_" + Calendar.getInstance().getTime(),** null);
        return Uri.parse(path);
    }

Now its a tag with IMG_+time in long

Solution 2:

Yes, this error is only in Android 10, and only in some device models.

All images are being stored as "Title" but need a unique name.

The accepted answer is not always generating a unique name if you are trying to store multiple images at the same time.

So replace the insertImage call:

String path = MediaStore.Images.Media.insertImage(
    inContext.getContentResolver(), inImage, "IMG_" + System.currentTimeMillis(), null
);