与 FileProvider 共享文件时权限被拒绝

Permission Denial while sharing file with FileProvider

我正在尝试与 FileProvider 共享文件。我检查文件是否与 Gmail、Google 驱动器等应用程序正确共享。即使抛出以下异常:

2019-08-28 11:43:03.169 12573-12595/com.example.name E/DatabaseUtils: Writing exception to parcel
    java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.example.name.provider/external_files/Android/data/com.example.name/files/allergy_report.pdf from pid=6005, uid=1000 requires the provider be exported, or grantUriPermission()
        at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:729)
        at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:602)
        at android.content.ContentProvider$Transport.query(ContentProvider.java:231)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:104)
        at android.os.Binder.execTransactInternal(Binder.java:1021)
        at android.os.Binder.execTransact(Binder.java:994)

提供商:

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

file_provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." />
</paths>

分享意向

Intent intentShareFile = new Intent(Intent.ACTION_SEND);
File fileWithinMyDir = new File(targetPdf);

if (fileWithinMyDir.exists()) {
    intentShareFile.setType("application/pdf");
    Uri uri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", fileWithinMyDir);
    intentShareFile.putExtra(Intent.EXTRA_STREAM, uri);
    intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "Sharing File...");
    intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File...");
    intentShareFile.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(intentShareFile, "Share File"));
}

希望您能指出我的错误,为什么当应用程序似乎已正确授予权限并且共享正常工作时会抛出此异常。

编辑:

我发现问题出在:

startActivity(Intent.createChooser(intentShareFile, "Share File"));

当我简单地把它改成

startActivity(intentShareFile);

但是,它显示的采摘应用程序布局略有不同。但是我还是不明白为什么原来的选择器不起作用。

抱歉回复晚了。我是这样解决的:

Intent chooser = Intent.createChooser(intentShareFile, "Share File");

List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);

for (ResolveInfo resolveInfo : resInfoList) {
    String packageName = resolveInfo.activityInfo.packageName;
    this.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}

startActivity(chooser);

这对我有帮助:Permission Denial with File Provider through intent

还有一种方法可以通过 Intent 标志授予 URI 权限,无需手动使用 grantUriPermission() 并保持 Intent.createChooser().

的用法

Intent.createChooser() 文档指出:

If the target intent has specified FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION, then these flags will also be set in the returned chooser intent, with its ClipData set appropriately: either a direct reflection of getClipData() if that is non-null, or a new ClipData built from getData().

因此,如果原始 Intent 在其 ClipDatasetData() 中设置了 Uri,它应该可以正常工作。

在您的示例中,ACTION_SEND Intent 支持从 Jelly Bean (Android 4.1) 开始通过 setClipData() 设置的 Uri。

长话短说,这是您的代码的一个工作示例:

Intent intentShareFile = new Intent(Intent.ACTION_SEND);
File fileWithinMyDir = new File(targetPdf);

if(fileWithinMyDir.exists()) {
    String mimeType = "application/pdf";
    String[] mimeTypeArray = new String[] { mimeType };
    
    intentShareFile.setType(mimeType);
    Uri uri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", fileWithinMyDir);
    
    // Add the uri as a ClipData
    intentShareFile.setClipData(new ClipData(
        "A label describing your file to the user",
        mimeTypeArray,
        new ClipData.Item(uri)
    ));
    
    // EXTRA_STREAM is kept for compatibility with old applications
    intentShareFile.putExtra(Intent.EXTRA_STREAM, uri);
    
    intentShareFile.putExtra(Intent.EXTRA_SUBJECT,
            "Sharing File...");
    intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File...");
    intentShareFile.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(intentShareFile, "Share File"));
}

这对我有用。

    Intent sharableIntent = new Intent();
    sharableIntent.setAction(Intent.ACTION_SEND);
    sharableIntent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION );

    Uri imageUri = Uri.parse(ImgLoc);
    File imageFile = new File(String.valueOf(imageUri));
    Uri UriImage = FileProvider.getUriForFile(context, Author, imageFile);

    sharableIntent.setType("image/*");
    sharableIntent.putExtra(Intent.EXTRA_STREAM, UriImage);
    sharableIntent.putExtra(Intent.EXTRA_TITLE, title);
    sharableIntent.putExtra(Intent.EXTRA_TEXT, body);

    Intent chooser = Intent.createChooser(sharableIntent, "Chooser Title");
    chooser.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    context.startActivity(chooser);

createChooser Intent.createChooser(): 构建一个新的 ACTION_CHOOSER 包装给定目标意图的意图,还可以选择提供标题。如果目标意图已指定 FLAG_GRANT_READ_URI_PERMISSION 或 FLAG_GRANT_WRITE_URI_PERMISSION,则这些标志也将在返回的选择器意图中设置,并适当设置其 ClipData:如果是 [=,则直接反映 getClipData() 16=],或从 getData()

构建的新 ClipData

除了上面nit的回答,还可以直接设置ClipData与要分享的uri如下。

intent.setClipData(ClipData.newRawUri("", uri));

这可以防止在呈现意向选择器时出现安全异常。

如果你想共享多个uris,你可以设置多个uris的clipdata来防止安全异常。总结:


Intent intent = new Intent();
intent.setType(mimeType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

if (uris.size() == 0) { 
   return;
}
else if (uris.size() == 1) {
   Uri uri = uris.get(0);
   intent.setAction(Intent.ACTION_SEND);
   intent.putExtra(Intent.EXTRA_STREAM, uri);
   intent.setClipData(ClipData.newRawUri("", uri));   
}
else {
  intent.setAction(Intent.ACTION_SEND_MULTIPLE);
  intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
  ClipData clipData = ClipData.newRawUri("", uris.get(0));

  for (int i = 1; i < uris.size(); i++) { 
      Uri uri = uris.get(i); 
      clipData.addItem(new ClipData.Item(uri));
  }
  intent.setClipData(clipData);

}

startActivity(Intent.createChooser(intent, title));