How to get the pdf file from assets in android?

2019-05-26 06:22发布

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.

2条回答
霸刀☆藐视天下
2楼-- · 2019-05-26 06:56

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

查看更多
Anthone
3楼-- · 2019-05-26 06:57

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.

查看更多
登录 后发表回答