简体   繁体   English

Android PDF 从内部存储打开后立即关闭

[英]Android PDF closes immediately after opening from Internal storage

Im trying to access a pdf file after downloading it from the server and storing it in internal storage.从服务器下载pdf文件并将其存储在内部存储器中后,我试图访问它。 The problem is after successfully downloading the file it opens for a split second then closes immediately.问题是在成功下载文件后,它打开一瞬间然后立即关闭。 Please can someone tell me whats the proper way to do it.请有人告诉我什么是正确的方法。 Thank you in advance.先感谢您。

Here's my code for downloading my pdf file:这是我下载pdf文件的代码:

File file = new File(getFilesDir(),"pdfFolder"); File file = new File(getFilesDir(),"pdfFolder");
if(!file.isDirectory()) file.mkdirs(); if(!file.isDirectory()) file.mkdirs();
File outputFile = new File(file, "samples.pdf"); File outputFile = new File(file, "samples.pdf");
//URLConnection.... //网址连接....
//InputStream.... //输入流....
FileOutputStream fileOutputStream = new FileOutputStream(outputFile); FileOutputStream fileOutputStream = new FileOutputStream(outputFile);

Here's my code for viewing my pdf file:这是我查看pdf文件的代码:

File pdfFile = new File(getFilesDir(),"/pdfFolder/samples.pdf"); File pdfFile = new File(getFilesDir(),"/pdfFolder/samples.pdf");
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);意图 pdfIntent = 新意图(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(Uri.fromFile(pdfFile),"application/pdf"); pdfIntent.setDataAndType(Uri.fromFile(pdfFile),"application/pdf"); pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(pdfIntent);开始活动(pdfIntent);

Note : the absolute path points to this "/data/user/0/com.package.name/pdfFolder/sample.pdf"注意:绝对路径指向这个“/data/user/0/com.package.name/pdfFolder/sample.pdf”

Try setting尝试设置

 pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

instead of代替

pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

It worked for me.它对我有用。

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

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