繁体   English   中英

无法通过邮件共享图像| FileProvider

[英]Unable share image through mail | FileProvider

尝试通过gmail共享图像时出现权限拒绝错误。

由以下原因引起:java.lang.SecurityException:权限拒绝:从ProcessRecord {fd38d47 28029:com.google.android.gm/u0a65}(pid = 28029,uid = 10065)打开提供程序android.support.v4.content.FileProvider不从uid 10378导出

private void sendMail() {
    Intent i = new Intent(Intent.ACTION_SENDTO);
    i.setData(Uri.parse("mailto:")); 
    i.putExtra(Intent.EXTRA_EMAIL, new String[]{"xyz@gmail.com"}); 
    i.putExtra(Intent.EXTRA_SUBJECT, "test");     
    File imagePath = new File(getFilesDir().getAbsolutePath(), "last_img");
    File newFile = new File(imagePath, "lastimg.jpg"); 
    Uri contentUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", newFile); 
    i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    i.putExtra(Intent.EXTRA_STREAM, contentUri);
    try {
        startActivity(Intent.createChooser(i, "Send mail...")); 
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
    }
}

供应商:

   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

provider_paths xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="lastImage" path="last_img/" />

共享的图像实际上是在应用程序内部创建的。

private void writeImage() {
        String root = context.getFilesDir().getAbsolutePath();
        File fileDir = new File(root + "/last_img");//no i18n
        File file = new File(fileDir, "lastimg.jpg");//no i18n
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        if (file.exists()) {
           file.delete();
        }
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file.getPath());

        fos.write(data);
        fos.close();
 }

任何帮助表示赞赏。

最后我通过改变解决了

 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
     String packageName = resolveInfo.activityInfo.packageName;
     grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}

请参阅此处以解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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