gpt4 book ai didi

java - 文件提供者 : Failed to find configured root

转载 作者:行者123 更新时间:2023-12-02 10:20:16 27 4
gpt4 key购买 nike

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

在主 list 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

最佳答案

The documentation for FileProvider <files-path> :

Represents files in the files/ subdirectory of your app's internal storage area. This subdirectory is the same as the value returned by Context.getFilesDir().

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

关于java - 文件提供者 : Failed to find configured root,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54393994/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com