Skip to content Skip to sidebar Skip to footer

How To Open Locally Saved Pdf File In An Android Webview?

I have tried to open a locally saved PDF file in a WebView. I can open it by using IntentService, but I want to open it in a WebView. Except for PDFs, all other files are working f

Solution 1:

You can use Google Docs Viewer to read your pdf online: webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);

Solution 2:

You can also use iframes.

pdfView.setWebChromeClient(new WebChromeClient());
WebSettings settings = pdfView.getSettings();
settings.setPluginState(WebSettings.PluginState.ON);
pdfView.setWebViewClient(new WebViewClient());
pdfView.setBackgroundColor(0x00000000);
settings.setAllowFileAccess(false);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(false);
settings.setSaveFormData(false);
settings.setJavaScriptEnabled(true);

String html = "<iframe src=\"http://docs.google.com/gview?url=" + 
    path + "&embedded=true&wmode=opaque\"" +
    "style=\"width:600px; height:900px;\" scrolling=\"no\"" + 
    "frameborder=\"0\" allowfullscreen></iframe>";

pdfView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);

Post a Comment for "How To Open Locally Saved Pdf File In An Android Webview?"