如何打开存储在内部存储器为PDF [英] How to open a PDF stored in Internal Memory

查看:140
本文介绍了如何打开存储在内部存储器为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,下载一个PDF文件,并​​存储与MODE_PRIVATE(出于安全目的)在App的内置记忆体:

 的FileOutputStream FOS = getApplicationContext()openFileOutput(LOCAL_FILE_NAME,Context.MODE_PRIVATE)。

InputStream中的InputStream = entity.getContent();
byte []的缓冲区=新的字节[1024];
INT BufferLength中= 0;

而(量(bufferLength = inputStream.read(缓冲液))大于0){
  fos.write(缓冲液,0,BufferLength中);
}

fos.close();
 

我怎么能打开并显示下载的PDF文件?我一直在寻找的计算器,我还没有找到一个清晰的解决方案。

看来,问题是,Android不具有原生支持来打开PDF文件,所以我认为有两种选择:

  1. 使用一些外部库来显示PDF文件。有没有什么行之有效?

  2. 启动的意图,以显示PDF文件,但它似乎为MODE_PRIVATE存储的文件不能由外部应用程序打开。我已经找到了一些解决方案,将文件复制到外部存储器,然后启动的意图,但此过程打破了MODE_PRIVATE选项的安全的目的,对吧?

感谢您的阅读

解决方案
  

我怎么能打开并显示下载的PDF文件?

创建一个的ContentProvider 服务PDF文件,然后用 ACTION_VIEW 意图乌里的ContentProvider 在用户选择的PDF查看器打开它。

演示了如何成为一个PDF文件示例项目从内部存储。

I have an App that downloads a PDF file and stores it with MODE_PRIVATE (for security purposes) on the Internal Memory of the App:

FileOutputStream fos = getApplicationContext().openFileOutput(LOCAL_FILE_NAME, Context.MODE_PRIVATE);

InputStream inputStream = entity.getContent();
byte[] buffer = new byte[1024];
int bufferLength = 0; 

while((bufferLength = inputStream.read(buffer)) > 0) {           
  fos.write(buffer, 0, bufferLength);      
}

fos.close();

How can I open and display the downloaded PDF file? I have been searching in StackOverflow and I haven't found a clear solution for this.

It seems that the problem is that Android doesn't has native support to open PDF files, so I think that there are two options:

  1. Use some external library to displays PDF files. Is there any that works well?

  2. Launch an Intent to show the PDF file, but it seems that the files stored as MODE_PRIVATE can't be opened by external Apps. I have found some solutions that copies the file to the external memory and then launches the Intent, but this procedure breaks the security purpose of the MODE_PRIVATE option, right?

Thanks for reading

解决方案

How can I open and display the downloaded PDF file?

Create a ContentProvider to serve the PDF file, then use an ACTION_VIEW Intent with the Uri to your ContentProvider to open it in the user's chosen PDF viewer.

This sample project demonstrates how to serve a PDF file from internal storage.

这篇关于如何打开存储在内部存储器为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆