使用资产文件夹中的其他应用程序打开 pdf,解决方法
-
26-12-2019 - |
题
在我的应用程序中,我的资产文件夹中存储了一些 pdf 文件。我见过用于打开 pdf 页面的库,但我认为像 Quickoffice 这样的应用程序比我见过的库更擅长显示 pdf。因此,我希望使用显示pdf Intent.ACTION_VIEW
, , 像这样:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "application/pdf");
getActivity().startActivity(intent);
但是,这是不可能的,因为不允许第三方应用程序访问我的包中的文件。因此,我需要将文件复制到外部存储并将该文件提供给意图。
这引出了我的问题:我的 pdf 非常大,所以我认为将它们存储两次(一次在我的资产文件夹中,一次在外部存储中)是愚蠢的。所以我想知道是否有解决方法。例如我可以这样做吗:
//Copy file to external storage
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "application/pdf");
getActivity().startActivity(intent);
//Delete file from external storage
这是一个很好的解决方法还是会导致 pdf 查看应用程序出现问题?或者有不同的解决方法吗?
解决方案
因此,我需要将文件复制到外部存储并将该文件提供给意图。
你也可以尝试 我的 StreamProvider
, 罐头 ContentProvider
, ,基于谷歌的 FileProvider
, ,来自资产。
对于多种资产,这应该适用于 StreamProvider
XML 元数据:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<asset
name="whatevs"/>
</paths>
这应该可以解决所有问题 content://your.authority.name.goes.here/whatevs/*
Uri
值(对于不同的值 *
) 到里面的文件 assets/
. 。如果您想将范围限制为某个特定子目录 assets/
(说, assets/goodstuff/
),你会使用:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<asset
name="whatevs"
path="goodstuff/"/>
</paths>
而且,如果这不起作用,它应该起作用,所以请随意 提出问题 具有可重现的测试用例。
这是一个很好的解决方法还是会导致 pdf 查看应用程序出现问题?
startActivity()
是异步的,因此外部 PDF 查看器将永远无法以这种方式访问文件。
不隶属于 StackOverflow