简体   繁体   English

如何在WebView中打开PDF / Docx

[英]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. 我试图允许用户单击pdf / doc链接,并能够直接在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. 这样可以正确打开Goog​​le Viewer(我也尝试过Google Viewer),但是显示的内容是HTML源代码,而不是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. 我不认为Android WebView会显示PDF,docx或其他“非本机”文档格式。 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. 您必须将URL传递给另一个应用程序才能查看它,或者自己下载它,然后将本地文件传递给另一个应用程序。

Use this line of Code: final String url = " http://docs.google.com/gview?embedded=true&url= " + myPdfUrl; 使用以下代码行:final String url =“ http://docs.google.com/gview?embedded=true&url= ” + myPdfUrl;

webView.loadUrl(url); webView.loadUrl(url);

that's all 就这样

Use Google doc support to show doc or excel , pdf, txt or other formate. 使用Google文档支持可显示doc或excel,pdf,txt或其他格式。

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);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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