I am shipping PDF files by placing them in the assets
folder of my android application. I am able to read the retieve the filenames using AssetManager
. I need to display them on screen and I am able to do that. But I need to open them using a pdfviewer.
To write an intent on onclick()
on each item, I need to pass the URI of the PDF file. And using the AssetManager
I cannot get the URI of the file. Is there a better way I can handle this whole thing?
Even if we were able to retrieve the URI, can the PDF viewer be able to read the PDF file in the assets folder?
UPDATE:
Based on the suggestion below , I wrote the following code
try {
Uri path = Uri.parse("android.resource://com.csg.android.myproject/raw/csgsample") ;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e){
Toast.makeText(FirstTab.this, "NO Viewer", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
It throws an ActivityNotFoundException
with the following exception message:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.csg.android.myproject/assets/pdf/csgsample typ=application/pdf flg=0x4000000 }
I queried the device using PackageManager
and I found the Adobe reader installed. But I am not sure why it is throwing this error. I also tried placing this in assets
folder instead and then changed the URI accordingly and ran the program but still I end up with the same error.
You can place them in the res/raw folder, and then create resource URIs to point to them. They are in the form of android.resource://com.example.myapp/raw/my_resource
. What I do not know for sure is if other apps such as a PDF reader would be able to open them. Try it out!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.