2017-10-19 18 views
0

我想打开PDF格式,这是interaly保存在应用程序/资产/ pdf_name.pdf从资产中分享PDF。 FileUriExposedException

我得到使用 File f = new File(getContext().getCacheDir() + /pdf_name.pdf");

我试图提取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

ALthout我可配置

<paths> 
    <cache-path 
     name="my_documents" 
     path="assets/"/> 
    <cache-path 
     name="my_pdf" 
     path="cache/"/> 
</paths> 

我的目标是通过意向来打开PDF,但我不能只是做:因为它抛出文件URI暴露例外

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 

回答

1

I get the file using

该代码与资产无关。资产是开发机器上的文件;它们不是设备上的文件,除非您手动将内容复制到文件中。

也许你的代码没有在问题中显示,你从资产中复制文件到new File(getContext().getCacheDir() + /pdf_name.pdf")

ALthout I've configurated

这两者都不适用于new File(getContext().getCacheDir() + /pdf_name.pdf")。删除一个<cache-path>元素,并从其他<cache-path>元素摆脱path属性,让你有:

<paths> 
    <cache-path name="my_pdf"/> 
</paths> 
+0

你save'd我的一天。我很接近,因为当我第一次输入姓名时,Android Studio强调错误:它应该包含路径,所以我放弃了这个想法。然而它编译,毕竟作品。 – murt