Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 408
Abraham Mathew Avatar asked May 07 '20 09:05

Abraham Mathew


1 Answers

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

like image 115
Abraham Mathew Avatar answered Nov 15 '22 08:11

Abraham Mathew