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

3

12

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);
}
Dentoid answered 7/5, 2020 at 9:11 Comment(2)
Do you have Storage permission?Cyma
Read and write permissions are thereDentoid
D
29

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

Dentoid answered 19/5, 2020 at 6:16 Comment(0)
G
18

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
);
Germiston answered 11/9, 2020 at 13:4 Comment(0)
A
0

Yes, I have Android 11 and It doesn't recognize the path when I upload the image from Camera. I have solved this by

  Random rand = new Random();
          int randNo = rand.nextInt(1000);
String path = MediaStore.Images.Media.insertImage(
    inContext.getContentResolver(), inImage, "IMG_" + randNo, null
);
Anglosaxon answered 11/1, 2022 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.