I’ve been searching everywhere for a solution to this problem. Most answers only show how to display remote PDF files using Google Docs viewer in a WebView, but nothing explains how to handle local PDF files stored on the device.
Here’s what I’m working with:
String serverURL = "https://example.com/documents/reports/invoice-2023-12345.pdf";
String devicePath = "file:///android_asset/MyApp/documents/invoice-2023-12345.pdf";
String docsViewer = "https://docs.google.com/gview?embedded=true&url=";
This approach works perfectly:
myWebView.loadUrl(docsViewer + serverURL);
But this doesn’t work at all:
myWebView.loadUrl(docsViewer + devicePath);
I noticed there might be encoding issues with special characters in the path, so I tried URL encoding:
String fixedPath = "file:///android_asset/MyApp%20Files/documents/invoice-2023-12345.pdf";
Still no luck. Can Google Docs viewer actually access local PDF files through WebView? What am I missing here?
google docs viewer just cant handle local files lol. i found using pdf.js in a webview is really easy for loading local pdfs. no need for external servers or crazy setups, just simple and works well.
Google Docs viewer can’t access files on your device - it needs public URLs that Google’s servers can reach. When you use a file:// path, Google’s service can’t fetch the content since it’s only on your local device. I hit this same issue on a project last year and had to work around it. You’ll need to serve the PDF through a temporary HTTP endpoint or move it to external storage with proper permissions, then use a content:// URI. Or just ditch Google Docs viewer for local files and use a dedicated PDF library like PDFRenderer or MuPDF instead.
Same exact frustration here when I built a document management app. Google Docs viewer runs on Google’s servers, so it can’t access files stored privately on your device. You’re basically asking a remote server to open a file that only exists on your phone - the file:// protocol means nothing to Google’s infrastructure.
I fixed this by setting up a local HTTP server using the NanoHTTPD library. Serve the PDF locally on localhost:8080, then pass that URL to Google’s viewer. It’s more complex but bridges the gap between local storage and remote services.
Alternatively, just use Android’s built-in PDF rendering if you don’t want the networking overhead.