Opening pdf with other app from assets folder, workarounds

我的未来我决定 提交于 2019-12-02 19:30:50

问题


In my app, I've got some pdf's stored in my assets folder. 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. Therefore, i wish to show the pdf using Intent.ACTION_VIEW, like this:

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). 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? Or is there a different work-around?


回答1:


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.

For multiple assets, this should work for the StreamProvider XML metadata:

<?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/. If you want to limit the scope to some specific subdirectory of assets/ (say, assets/goodstuff/), you would use:

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

startActivity() is asynchronous, and so the external PDF viewer will never be able to access the file this way.



来源:https://stackoverflow.com/questions/21063259/opening-pdf-with-other-app-from-assets-folder-workarounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!