简体   繁体   中英

Opening a PDF in an Android WebView

I have an android app that includes a WebView. When a user clicks on a link to a pdf nothing happens in the app. After some research it seems that the WebView doesn't support showing PDF files out of the box.

I've looked at several solutions on SO and all of them seem to use deprecated code.

What is the best way to accomplish this? I would add that I am fine with another app opening the pdf (it doesn't have to display directly in my app).

Edit: Clarification

Due to security concerns, pointing our pdf url to docs.google.com is not allowed.

Unfortunately, Android doesn't support viewing PDFs in a WebView. But still, you can do it in some other way. Actually, you can use the Google Docs. We will be loading the PDF in the Google Docs.

Here is the Code Snippet which you can use to open the PDF-

String myPdfUrl = "http://example.com/awesome.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl;
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl(url);

That's all. Your Pdf will be open in the Google Docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM