Android PDF closes immediately after opening from Internal storage

那年仲夏 提交于 2019-12-23 23:08:23

问题


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.

Here's my code for downloading my pdf file:

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);

Here's my code for viewing my pdf file:

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);

Note : the absolute path points to this "/data/user/0/com.package.name/pdfFolder/sample.pdf"


回答1:


Try setting

 pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

instead of

pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

It worked for me.



来源:https://stackoverflow.com/questions/41788650/android-pdf-closes-immediately-after-opening-from-internal-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!