Android: How to use grantUriPermission to be able to create and send an email with a bitmap attachment
android, bitmap, java
Solution
Try including this in your AndroidManifest.xml.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
On Android 6.0+ the permission must be granted by the user to the application otherwise this will throw the SecurityException.
To do this go into Settings/Apps/[AppName]/Permissions and allow the Storage Permission.
Problem
From within my application, I'm trying to create an email that contains an image contained in a bitmap object. ``` private void sendEmailWithBitmapAttached(){ final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType("plain/text"); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Email Subject"); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Email Body"); emailIntent.setType("image/png"); ContentResolver cr = getContentResolver(); // insert the image and create a path String imageBitmapPath = MediaStore.Images.Media.insertImage(cr, bitmapForEmail,"title", "description"); // create a uri Uri imageUri = Uri.parse(imageBitmapPath); emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); emailIntent.putExtra(Intent.EXTRA_STREAM, imageUri); // send the email startActivity(Intent.createChooser(emailIntent, "Send mail...")); } ``` This works fine in Android 2.3. But using later versions, it produces the following error: ``` 07-13 23:01:01.252: E/MediaStore(5194): Failed to insert image 07-13 23:01:01.252: E/MediaStore(5194): java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=5194, uid=10151 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission() ``` So, taking the suggestion of the error message, I tried to grantUriPermission. ``` grantUriPermission(String toPackage, Uri uri, int modeFlags) ``` But I am not sure what to put for toPackage or uri But again, using the error message, I modified the code as follows: ``` private void sendEmailWithBitmapAttached(){ final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType("plain/text"); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Email Subject"); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Email Body"); emailIntent.setType("image/png"); ContentResolver cr = getContentResolver(); // create a Uri for the content provider suggested by the error message Uri uri = Uri.parse("content://media/external/images/media"); // create a package provider string suggested by the error messge. String provider = "com.android.providers.media.MediaProvider"; // grant all three uri permissions! grantUriPermission(provider, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); grantUriPermission(provider, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION); grantUriPermission(provider, uri, Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); // insert the image and create a path String imageBitmapPath = MediaStore.Images.Media.insertImage(cr, bitmapForEmail,"title", "description"); // create a uri Uri imageUri = Uri.parse(imageBitmapPath); emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); emailIntent.putExtra(Intent.EXTRA_STREAM, imageUri); // send the email startActivity(Intent.createChooser(emailIntent, "Send mail...")); } ``` And I get the exact same error. Can a kind soul please give me a hint as how to take care of grantUriPermission's uri and provider items? Is this the correct approach? Thank you very much for ANY help, hint, guides, or suggestion, you can provide!