4
votes

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

1 Answers

0
votes

Try setting

 pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

instead of

pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

It worked for me.