I want to open a PDF in my WebView, and I found and combined codes on this forum.
But it catches the "No PDF application found" although I have multiple PDF apps installed, including Adobe Reader.
Here the code:
private class PsvWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (url.contains(".pdf")) {
Uri path = Uri.parse(url);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(PsvWebViewActivity.this, "No PDF application found", Toast.LENGTH_SHORT).show();
}
catch(Exception otherException)
{
Toast.makeText(PsvWebViewActivity.this, "Unknown error", Toast.LENGTH_SHORT).show();
}
}
return true;
} } }
Opening a PDF file in Android using WebView All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.
We can load PDF in android easily using WebView. We will be using the simplest way for displaying PDF file in android. Using the Google Docs PDF viewer we could do this easily. Now lets get in to the implementation and source code.
If possible, you can display PDF documents and visit online websites using the add-in: import 'dart:html' as html; html. window. open('http:///www.website.com/document.pdf');
(1) Google Docs Viewer, You can open it in android Browser like,
mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+ webUrl);
Update:
(2) Check this library, in build.gradle(app module) add this dependency,
compile 'com.github.barteksc:android-pdf-viewer:2.8.2'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With