从我的应用程序内部,我试图创建一封电子邮件,其中包含位图对象中包含的图像。

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..."));
}

在Android 2.3中可以正常工作。

但是使用更高版本时,它会产生以下错误:
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()

因此,根据错误消息的建议,我尝试授予GrantUriPermission。
grantUriPermission(String toPackage, Uri uri, int modeFlags)

但是我不确定要放入toPackage或uri

但是再次,使用错误消息,我修改了代码,如下所示:
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..."));
}

而且我得到完全相同的错误。

请问一个善良的灵魂给我一个提示,说明如何处理GrantUriPermission的uri和provider项目?这是正确的方法吗?

非常感谢您提供的任何帮助,提示,指南或建议!

最佳答案

尝试将其包含在AndroidManifest.xml中。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

在Android 6.0+上,用户必须将权限授予应用程序,否则将引发SecurityException。

为此,请进入“设置/应用程序/[AppName]/权限”并允许“存储权限”。

08-17 16:02