Reputation: 3442
I want to read a PDF file in android. I placed my PDF files in the assets folder.
How can i read the PDF file from there?
I have checked the above link but it does not work for me. It gives me an error saying that the Activity was not found.
And I also want to open a PDF file in WebView. So is it possible to read PDF in WebView?
Upvotes: 11
Views: 35815
Reputation: 1132
private void openPDF(final String path) {
File file = new File(path);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
} else {
uri = Uri.fromFile(file);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 0
Reputation:
Another great solution to read pdf using externally installed application like Adobe Reader
.
private void openPDF(final String pathToPDF) {
File file = new File(pathToPDF);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 4
Reputation:
Google cached displays formats like pdf, ppt, docx, etc. And you don't need to install any application at all. Search the link in google and go to Cached.
Upvotes: 0
Reputation: 4783
You need to have a application which can support that mimetype and open it. In your device/emulator that app is not installed so it is throwing error
Upvotes: 8