Reading a pdf file placed in assets folder in andr

2019-07-27 08:26发布

问题:

I have a pdf file placed in my assets folder and I am firing an intent to read it. Here is my code :

 Uri path = Uri.parse("file:///android:asset/about.pdf");
   Intent intent  = new Intent(Intent.ACTION_VIEW);
   intent.setDataAndType(path, "application/pdf");
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   startActivity(intent);

When I do this, apop up appears with list of applications including adobe reader but when I click on it, a dialog appears saying :"Error", the file path is not valid

Please tell me how to fix this

回答1:

Is this file in your app's assets folder? If it is, then your URI is wrong, check out this question:

How to open a pdf stored either in res/raw or assets folder?

there's an answer describing how to construct a proper URIs for your assets.