How to get the pdf file from assets in android?

2019-05-26 06:31发布

问题:

In my application, I am getting file from assets resources and open pdf file from it. I am using this code:

Uri path = Uri.parse("file:///android_asset/WindsorONE_Mobile_PK.pdf"); 

            Intent intentPDF = new Intent(Intent.ACTION_VIEW);                     
            intentPDF.setDataAndType(path, "application/pdf");                     
            intentPDF.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);                      
            try {                         
                startActivity(intentPDF);                     
            }                      
            catch (ActivityNotFoundException e) {                         
                Toast.makeText(ListSample.this,                              
                     "No Application Available to View PDF",                              
                     Toast.LENGTH_SHORT).show();                     

            }   

But While I run that code. I got error Dialog that:

"File path is not valid"

So where I am wrong ? Please help me for that.

Thanks.

回答1:

The problem is that you're passing a resource path from your application to a third party application, but the path is only valid from within your application since it's for a private resource.

The only solution for this is to copy that PDF file from assets to external storage, then give the path to that file (the one in external storage) to the third party app. Then it should work.



回答2:

You need to use the asset manager for accessing the items in assets.Link is here