0

I am trying to load a PDF file via WebView. I tried almost all the answers here in StackOverflow but still failed. Here is my resulting code:

WebView webView = findViewById(R.id.webView);
WebSettings settings = webView.getSettings();
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBuiltInZoomControls(true);
webView.setContentDescription("application/pdf");
webView.loadUrl("file:///data/user/0/com.example.pdfwebviewer/files/masterlist.pdf");

I derived this code from a couple of sources:

  1. How to open local PDF file in WebView in Android?
  2. Can't open a local PDF in webview using file://
  3. Open PDF in a WebView

What did I miss?

user1506104
  • 6,554
  • 4
  • 71
  • 89

2 Answers2

1

It seems you have to try to use some library, because android-WebView doesn't seem to support .pdf files.

  1. Mentioned here
  2. Mentioned here in a comment to the main question

So what Library

Alot of answers on this site like this one here recommends this library to read local pdf files:

AndroidPdfViewer

Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
-1

This solution works for me.

https://stackoverflow.com/a/9139859/5653463

looks like this:

val googleDocs = "https://docs.google.com/gview?embedded=true&url="
view.loadUrl("googleDocs${URLEncoder.encode(url.toString()"ISO-8859-1")}")