从资产的文件夹中打开PDF与其他应用程序,解决方法解决方法、应用程序、资产、文件

由网友(爱就疯狂、不爱就坚强)分享简介:在我的应用程序,我有存储在我的资产文件夹一些PDF格式的。我已经看到了打开PDF,网页库,但我认为,如Quickoffice的应用程序是在显示PDF格式的,比我见过的图书馆更好。因此,我希望用显示PDF Intent.ACTION_VIEW ,像这样的:意向意图=新的意图(Intent.ACTION_VIEW);i...

在我的应用程序,我有存储在我的资产文件夹一些PDF格式的。我已经看到了打开PDF,网页库,但我认为,如Quickoffice的应用程序是在显示PDF格式的,比我见过的图书馆更好。因此,我希望用显示PDF Intent.ACTION_VIEW ,像这样的:

 意向意图=新的意图(Intent.ACTION_VIEW);
intent.setDataAndType(了fileURI,应用程序/ PDF格式);
。getActivity()startActivity(意向);
 

但是,这是不可能的,因为第三方应用不允许存取权限在我的包文件。因此,我需要将文件复制到外部存储,并提供该文件的意图。

这使我对我的问题:我的PDF文件的尺寸比较大,所以我觉得这是愚蠢来存储它们两次(一次在我的资产文件夹,一旦外部存储)。所以我想知道如果有一个工作围绕这个。我能例如做的:

  //将文件复制到外部存储
意向意图=新的意图(Intent.ACTION_VIEW);
intent.setDataAndType(了fileURI,应用程序/ PDF格式);
。getActivity()startActivity(意向);
//删除从外部存储文件
 
如何把word文档转成pdf文档

这是一个很好的解决方法还是将这项事业问题与PDF的浏览应用程序吗?还是有不同的解决方法?

解决方案   

因此​​,我需要将文件复制到外部存储,并提供该文件的意图。

您也可以尝试我的 StreamProvider ,罐头的ContentProvider ,基于谷歌的 FileProvider ,即流从资产。

有关多个资产,这应该工作,为 StreamProvider XML元数据:

 < XML版本=1.0编码=UTF-8&GT?;
<路径的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <资产
        NAME =whatevs/>

< /路径>
 

这应该可以解决所有的内容://your.authority.name.goes.here/whatevs/* 乌里值(为 * 不同的值),以文件的资产/ 。如果你想的范围限制在某些特定的子目录资产/ (例如,资产/ goodstuff / ),你将使用:

 < XML版本=1.0编码=UTF-8&GT?;
<路径的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <资产
        NAME =whatevs
        路径=goodstuff //>

< /路径>
 

和,如果这也不行,它应该,所以随时文件中的问题具有可再生的测试用例。

  

这是一个很好的解决方法还是将这项事业问题与PDF的浏览应用程序吗?

startActivity()是异步的,所以外部PDF浏览器将永远无法访问该文件这样。

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?

解决方案

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.

阅读全文

相关推荐

最新文章