Reputation: 600
I was wondering can anyone help me with a problem that I'm having. I've looked at many posts of the same issue but I still cannot resolve this problem. I have created a PDF document. I have placed my created PDF file into my res/raw folder (I have also tried placing in assets folder) and I'm trying to open it within my app. When I click the button, it opens the dialog that I can select to complete the action using adobe reader, amazon kindle or polaris office. But when I go to try and open the pdf, I get an error saying "The document cannot be opened because it is not a valid PDF document".
The following is the code I am using to try and open the pdf, has anyone any ideas on why this will not work?
String fileName = "android.resource://" + getPackageName() + "/" + R.raw.user_manual_v1_0_0;
File file = new File(fileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);
Upvotes: 1
Views: 10396
Reputation: 83
Be sure to not forget the permissions for your app to reach external files. Place these two lines in your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Upvotes: 0
Reputation: 1006564
It will not work because few, if any, PDF viewers are set up to open PDFs from resources, and none can read directly from assets.
You will be better served copying the file to the filesystem. If you copy it to external storage, you can just open up a PDF viewer on that. If you copy it to internal storage -- by default, private to your app -- you can create a ContentProvider
to serve the PDF to the viewer. This sample application demonstrates the latter approach.
Upvotes: 4