FileProvider:无法找到配置的根

时间:2019-01-27 23:48:40

标签: java android android-studio

我一直在尝试共享pdf文件,我这样设置FileProvider:

在主清单xml上:

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

res / xml / file_paths.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="my_files" path="." />
</paths>

在我的代码中,我尝试以下操作:

String path = root.getAbsolutePath() + "/file.pdf";
final Uri data = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".fileprovider", new File(path));
getApplicationContext().grantUriPermission(getApplicationContext().getPackageName(), data, Intent.FLAG_GRANT_READ_URI_PERMISSION);
final Intent intent = new Intent(Intent.ACTION_VIEW).setDataAndType(data, "application/pdf").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
getApplicationContext().startActivity(intent);

返回错误:

     Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/file.pdf

1 个答案:

答案 0 :(得分:2)

The documentation for FileProvider<files-path>

  

代表应用程序内部存储区域的files/子目录中的文件。该子目录与Context.getFilesDir()返回的值相同。

您的文件不在internal storage中。它在external storage中。为此,您需要一个<external-path>元素,而不是一个<files-path>元素。