简体   繁体   English

从资产文件夹中使用其他应用程序打开pdf文件,解决方法

[英]Opening pdf with other app from assets folder, workarounds

In my app, I've got some pdf's stored in my assets folder. 在我的应用中,我的资产文件夹中存储了一些pdf文件。 I've seen libraries for opening pdf-pages, but I think that apps such as quickoffice are better at showing the pdf's than the libraries I've seen. 我见过用于打开pdf页面的库,但我认为Quickoffice之类的应用程序比我见过的库更能显示pdf。 Therefore, i wish to show the pdf using Intent.ACTION_VIEW , like this: 因此,我希望使用Intent.ACTION_VIEW显示pdf,如下所示:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "application/pdf");
getActivity().startActivity(intent);

However, this is not possible because third party apps are not allowed to acces files in my package. 但是,这是不可能的,因为不允许第三方应用访问我程序包中的文件。 Therefore I need to copy the files to the external storage and provide that file to the intent. 因此,我需要将文件复制到外部存储中,然后将其提供给意图。

That brings my to my question: my pdf's are quite big in size, so I think it'd be stupid to store them twice (once in my assets folder and once on the external storage). 这使我想到了一个问题:我的pdf很大,因此我认为将它们存储两次(一次在我的Assets文件夹中,一次在外部存储器中)是愚蠢的。 So I'm wondering if there is a work-around for this. 所以我想知道是否有解决方法。 Could i for example do: 我可以例如这样做:

//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

Is this a good work-around or will this cause problems with the pdf-viewing app? 这是一个好的解决方法,还是会导致pdf查看应用出现问题? Or is there a different work-around? 还是有其他解决方法?

Therefore I need to copy the files to the external storage and provide that file to the intent. 因此,我需要将文件复制到外部存储中,然后将其提供给意图。

You can also try my StreamProvider , a canned ContentProvider , based on Google's FileProvider , that streams from assets. 您还可以尝试我的StreamProvider ,它是基于Google的FileProvider的罐装ContentProvider ,可以从资产流式传输。

For multiple assets, this should work for the StreamProvider XML metadata: 对于多个资产,这应该适用于StreamProvider XML元数据:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">

    <asset
        name="whatevs"/>

</paths>

That should resolve all content://your.authority.name.goes.here/whatevs/* Uri values (for various values of * ) to files inside of assets/ . 那应该将所有content://your.authority.name.goes.here/whatevs/* Uri值(用于*各种值)解析为assets/文件。 If you want to limit the scope to some specific subdirectory of assets/ (say, assets/goodstuff/ ), you would use: 如果要将范围限制为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>

And, if this does not work, it should, so feel free to file an issue with a reproducible test case. 并且,如果这不起作用,则应该随便提出一个可复制的测试用例的问题

Is this a good work-around or will this cause problems with the pdf-viewing app? 这是一个好的解决方法,还是会导致pdf查看应用出现问题?

startActivity() is asynchronous, and so the external PDF viewer will never be able to access the file this way. startActivity()是异步的,因此外部PDF查看器将永远无法以这种方式访问​​文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM