0

I have following code:

Uri u_img = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(
getContentResolver(),f.getAbsolutePath(), null, null));

but android.provider.MediaStore.Images.Media.insertImage returns null on Galaxy S6 6.0.1.

I have tried following possible solutions but no luck:

  1. Checking permission of Writing to External Storage -> OK
  2. Try to do the checking of directory is there or not by f.exists() and it returns true

What could be another reason?

Rendy
  • 5,572
  • 15
  • 52
  • 95

2 Answers2

0
public  void addImageToGallery(final String filePath, final Context context) {

    ContentValues values = new ContentValues();

    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    values.put(MediaStore.MediaColumns.DATA, filePath);

    context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
Adeel Turk
  • 897
  • 8
  • 23
0

you are encountering this problem in android Marshmallow, as you said on Galaxy S6 6.0.1. This is because of permissions.you need to request for write permission as MediaStore.Images.Media.insertImage try to write on sd card. Open a Dialog using the code below:

ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                1);

Also this link can help you with permission in android 6(Marshmallow) https://developer.android.com/training/permissions/requesting.html

Hadi Samadbin
  • 237
  • 3
  • 17