如何打开存储在内部存储器为PDF存储器、在内部、PDF

2023-09-07 15:26:00 作者:梦奴

我有一个应用程序,下载一个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文件,所以我认为有两种选择:

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

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

感谢您的阅读

解决方案   

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

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

在表示计算机存储容量时,1GB相当于多少

这演示了如何成为一个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:

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

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.

 
精彩推荐