Search code examples
androidfiledirectory-structure

I am downloading pdf's to internal storage but my pdf viewer can not find them


I download a pdf file to internal storage and then programmatically create buttons that take them to an action view. I can listFiles[] and Toast that they are there but the pdf viewer says file does not exist or file can not be viewed.

These are the main components of the write to internal storage during download.

private File file;
file = new File(mContext.getFilesDir(), filename+".pdf");
// Output stream to write file in internal storage
                    OutputStream output = new BufferedOutputStream(new FileOutputStream(file));

Then in another activity I get the filenames from the database and create a table with buttons

// ....Inside a for loop ..... 
Button c3 = new Button(this);
        c3.setText("view");
        c3.setId(p.getPosterID());
        c3.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                File pdfFile = getBaseContext().getFileStreamPath(filename+".pdf");
                if (pdfFile.exists()){
                    Uri path = Uri.fromFile(pdfFile);
 //================================================================================
                    Log.d("path: ", path.toString());
    //this will log: file:///data/data/com.myapp.posterviewer/files/5453b54b83b5f.pdf
 //================================================================================
                    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                    pdfIntent.setDataAndType(path, "application/pdf");
                    pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    try{
                        startActivity(pdfIntent);
                    }
                    catch(ActivityNotFoundException e){
                        Toast.makeText(ListPosters.this, "No Application Available", Toast.LENGTH_LONG).show();
                    }
                }
                else{
                    Toast.makeText(ListPosters.this, "The file does not exist", Toast.LENGTH_LONG).show();
                }
            }
        });

Am I generating the path right with Uri path? I am not able to see the files from windows explorer when in the app folder either. But all my checks say file is exists and I thought that would be in the app root folder.


Solution

  • Internal Storage Only Visible to your Application. The OutSide Application(Pdf Viewer) Cannot Access it. Save you Pdf file in External Storage and then open it via Pdf Viewer