简体   繁体   中英

How to Open PDF/Docx in WebView

I'm trying to allow the user to click on a pdf/doc link and be able to see it directly inside the webview. This is my code:

webView.setDownloadListener(new DownloadListener(){
    @Override
    public void onDownloadStart(String url, String userAgent, 
            String contentDisposition, String mimetype, long contentLength) {
        webView.loadUrl("https://docs.google.com/gview?embedded=true&url="+url);
    }
});

This correctly opens up a Google Viewer (I've also tried Google Viewer), but the contents that show up are the HTML source code, not the pdf/doc. Can someone tell me what I'm doing wrong with this?

I don't believe the Android WebView knows how to display PDF, docx, or other "non-native-Web" document formats. You'd have to pass the URL to another application to view it, or download it yourself and pass the local file to the other application.

Use this line of Code: final String url = " http://docs.google.com/gview?embedded=true&url= " + myPdfUrl;

webView.loadUrl(url);

that's all

Use Google doc support to show doc or excel , pdf, txt or other formate.

WebView urlWebView = (WebView)findViewById(R.id.containWebView);
urlWebView.setWebViewClient(new AppWebViewClients());
urlWebView.getSettings().setJavaScriptEnabled(true);
urlWebView.getSettings().setUseWideViewPort(true);
urlWebView.loadUrl("http://docs.google.com/gview?embedded=true&url="
                + "YOUR_DOC_URL_HERE"); 

public class AppWebViewClients extends WebViewClient {



    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

    }
}

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