有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java FileProvider:找不到配置的根目录

我一直在尝试共享一个pdf文件,我将文件提供程序设置为:

在主清单xml上:

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

res/xml/file_路径。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

共 (0) 个答案