从内部存储打开后,Android PDF立即关闭 [英] Android PDF closes immediately after opening from Internal storage
问题描述
从服务器下载pdf文件并将其存储在内部存储器后,我试图访问pdf文件.问题是在成功下载文件后,它会打开一秒钟,然后立即关闭.请有人告诉我正确的做法是什么.先感谢您.
Im trying to access a pdf file after downloading it from the server and storing it in internal storage. 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.
这是我下载pdf文件的代码:
Here's my code for downloading my pdf file:
文件文件=新文件(getFilesDir(),"pdfFolder");
if(!file.isDirectory())file.mkdirs();
文件outputFile =新文件(文件, "samples.pdf");
//URLConnection ....
//InputStream ....
FileOutputStream fileOutputStream =新的FileOutputStream(outputFile);
File file = new File(getFilesDir(),"pdfFolder");
if(!file.isDirectory()) file.mkdirs();
File outputFile = new File(file, "samples.pdf");
//URLConnection....
//InputStream....
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
这是查看我的pdf文件的代码:
Here's my code for viewing my pdf file:
文件pdfFile =新文件(getFilesDir(),"/pdfFolder/samples.pdf");
意图pdfIntent =新意图(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(Uri.fromFile(pdfFile),"application/pdf"); pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(pdfIntent);
File pdfFile = new File(getFilesDir(),"/pdfFolder/samples.pdf");
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(Uri.fromFile(pdfFile),"application/pdf"); pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(pdfIntent);
注意:绝对路径指向此"/data/user/0/com.package.name/pdfFolder/sample.pdf"
Note : the absolute path points to this "/data/user/0/com.package.name/pdfFolder/sample.pdf"
推荐答案
尝试设置
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
代替
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
对我有用.
这篇关于从内部存储打开后,Android PDF立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!