【问题标题】:Pdf File In asset folder not open in thirdparty app资产文件夹中的 Pdf 文件未在第三方应用程序中打开
【发布时间】:2017-01-25 15:43:14
【问题描述】:

嗨,我是 android 新手,我有一些 PDF 文件存在于此路径 asset/pdf/example.pdf

当我尝试使用此代码的 PDF 文件时

Intent intent = new Intent(Intent.ACTION_VIEW);
    File file = new File("file:///android_asset/pdf/example.pdf");
    intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
    startActivity(intent);

我收到此错误并知道这是因为第三方无权访问文件 什么是解决方案?

【问题讨论】:

    标签: android android-assets


    【解决方案1】:

    知道这是因为第三方无权访问文件

    file:///android_asset/ 仅适用于 WebView AFAIK。

    什么是解决方案?

    您可以使用my StreamProvider,它提供了一个ContentProvider,可以提供来自assets/ 的文件。

    或者,您可以实现自己的ContentProvider,它可以提供来自assets/ 的文件。

    或者,您可以将资产复制到本地文件(例如,getFilesDir()getCacheDir())、then use FileProvider to serve that local file

    【讨论】:

    • 伙计,我在提问之前也阅读了您的 GITHUB 项目,但无法再次处理!我是新手,不知道如何使用流提供程序,您可以根据我的文件路径编写并举个例子吗?
    • @MohamadAmin: The StreamProvider demo/ project 演示了从资产提供文件等。我书中的许多拖放示例也是如此,例如this oneStreamProvider 用于drag/ 应用模块)。
    • 我无法处理 :( 尝试了两个小时
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多