1

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 for me.

I have used the codes below:

WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setAllowFileAccess(true);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(mFilePath));
webView.loadData(mFilePath, mimeType, "UTF-8");

Can anyone please help? Thanks in advance.

ChuongPham
  • 4,761
  • 8
  • 43
  • 53
Shafi
  • 211
  • 4
  • 13

2 Answers2

0

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);

BeataH
  • 11
  • 5
0

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);
BeataH
  • 11
  • 5