简体   繁体   中英

Converting bitmap Image to Uri not working

I am Using following code to convert bitmap Image to Uri

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

Its working fine in Devices above from Marshmellow but not working in below versions

MediaStore.Images.Media.insertImage

Mentioned above code in the method is returning null in case of failure.I searched through the internet but could not get any rightful answer. Hoping for proper guidelines.

I am using karbon 5.1 (API 22)

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 and android.permission.READ_EXTERNAL_STORAGE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM