How to Open Local Pdf File in Webview in Android

How to open local pdf file in WebView Android Studio

I have a pdf in the raw folder and I am trying to open it in a webview using "https://drive.google.com/viewerng/viewer?embedded=true&url=" to open it in the webview.

That has never been possible.

Is there a better way to load a local pdf inside my app without having it take up the entire activity page?

The best thing is to not load it inside your app at all. Use the user's preferred PDF viewing app, via ACTION_VIEW.

Beyond that, you could use a slightly modified version of PDF.js on Android 4.4+. Or, use AndroidPdfViewer on a wider range of Android versions, though it makes your APK a lot bigger. See this blog post for more context on these and other (worse) options.

Also, here are sample apps demonstrating:

  • PDF.js: https://github.com/commonsguy/cw-omnibus/tree/v8.10/PDF/PdfJS
  • AndroidPdfViewer: https://github.com/commonsguy/cw-omnibus/tree/v8.10/PDF/Pdfium

Open local pdf file on webview

I would suggest you use the native feature PdfRenderer instead of webview.

From android OS 5.0(lollipop) on-wards you can use this class to show pdf's within the app. If you want to support OS lower than that you can use a library as there is no native support

Read more about the class from the docs here, you can also refer this example provided by google

Pdf file from my device does not load in WebView

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

Load sdcard pdf files in Webview in android

Webview cant load local(sdcard) pdf file,
If you want to load sdcard pdf file you can use third party lib for it

https://github.com/barteksc/AndroidPdfViewer

or

You can use this method if you want

https://stackoverflow.com/a/43087727/4951663

How to open internal storage files in webview?

I have tried using Content Provider but it not works for PDF and MS-Word files.

A ContentProvider works equally well for all MIME types.

I tried the below code for open in WebView.

That is using an undocumented, unsupported URL for displaying Web-hosted content in a Google-supplied document-rendering page. That has no ability to access files on a device.

How can I open internally stored files in webview?

For certain types of files — such as plain text, HTML, and some image formats — WebView will be able to display the content directly. Just use loadUrl() with the file URL.

For a few other types of files, you may be able to find third-party JavaScript that you can use. For example, for PDF, you could modify Mozilla's PDF.js to be able to work with local PDFs.

The only general-purpose solution is for you to learn JavaScript and write your own document-rendering code. Put that JavaScript (and related HTML/images/CSS) in your Android app, and have it open the local filesystem content using file URL values.



Related Topics



Leave a reply



Submit