android - 从 Assets 共享 PDF。 FileUriExposedException 异常

标签 android android-assets android-fileprovider

我想打开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/

相关文章:

android - 无法在 Android 平台上初始化 ZXing - Xamarin Forms 应用程序

Android:向 ArrayAdapter 添加数据时可见的 ListView 图像闪烁

android - 从 asset 或 raw 文件夹中获取多个图像的资源标识符 (resId) 到一个 int 数组

java - android.os.FileUriExposeException : at android. os.StrictMode.onFileUriExpose

android - 如何在 Xamarin (Android) 中添加 fileprovider xml 文件

java - Android Studio Proguard 运行失败

java - Android没有定义GL_DEPTH_COMPONENT24?

android - 覆盖Android项目 Assets 文件夹中文本文件的内容

java - 漏窗错误

android - 将相机图像保存到应用程序包文件夹