我想打开PDF,它在interaly保存在app/assets/pdf_name.pdf
我使用
File f = new File(getContext().getCacheDir() +/pdf_name.pdf");
如果使用 content:// 形式,我正在尝试提取 URI
Uri pdfUri = FileProvider.getUriForFile(getContext(), "pcg_name.fileprovider", file);
但是我得到了
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/pck_name/cache/pdf_name.pdf
虽然我已经配置了
<paths>
<cache-path
name="my_documents"
path="assets/"/>
<cache-path
name="my_pdf"
path="cache/"/>
</paths>
我的目标是通过 Intent 打开 PDF,但我不能这样做:
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(pdfFile),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
target.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // DON'T FORGET this
因为它抛出 File Uri exposed 异常
最佳答案
I get the file using
该代码与 Assets 无关。 Assets 是您开发机器上的文件;它们不是设备上的文件,除非您手动将内容复制到文件中。
也许您有代码(未在问题中显示)将文件从 assets 复制到 new File(getContext().getCacheDir() + /pdf_name.pdf")
.
ALthout I've configurated
这些都不适用于 new File(getContext().getCacheDir() + /pdf_name.pdf")
.删除一个<cache-path>
元素并摆脱 path
来自另一个的属性<cache-path>
元素,留给你:
<paths>
<cache-path name="my_pdf"/>
</paths>
关于android - 从 Assets 共享 PDF。 FileUriExposedException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46829001/