Open PDF in a WebView
(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'
I know, this question is old.
But I really like the approach of Xamarin to make use of the pdf.js from Mozilla. It works on older Android versions, you don't need a special PDF Viewer app for this and you can easily display a PDF inside of your apps views hierarchy.
Git for this: https://mozilla.github.io/pdf.js/
Additional options: https://github.com/mozilla/pdf.js/wiki/Viewer-options
Just add the pdfjs files to your Assets directory:
And call it the following way:
// Assuming you got your pdf file:
File file = new File(Environment.getExternalStorageDirectory() + "/test.pdf");
webview = (WebView) findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBuiltInZoomControls(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + file.getAbsolutePath());
Cool thing: If you want to reduce the amount of functionalities / controls. Go to the Assets/pdfjs/web/viewer.html file and mark certain controls as hidden. With
style="display: none;"
E.g. If you don't like the right toolbar:
<div id="toolbarViewerRight" style="display: none;">...</div>
Update: This will open built-in app (PDF Viewer) to display pdf file if OS >= Marshmallow (API 23), otherwise will open the browser to display pdf using "docs.google.com" server.
WebView webView = new WebView(this);
String url = "http://www.pdf995.com/samples/pdf.pdf";
// Support all types of OS versions.
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
webView.loadUrl(url); // This will open it with a built-in PDF viewer app.
else
webView.loadUrl("http://docs.google.com/viewerng/viewer?url="+ url); // This will open it with a browser. On Android 5.0 (Api 21 - Lollipop) and bellow.
// Set Download listener.
webView.setDownloadListener((url1, userAgent, contentDisposition, mimetype, contentLength)
-> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url1))));