Webview未从URL加载PDF

时间:2018-04-02 09:32:44

标签: android webview

Webview未从网址加载PDF。我正在预览不可用

请对此表示感谢。

private void loadUrl() {
            String reportStatus = caseList.get(position).getReportStatus();
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.setWebViewClient(new WebViewClient() {

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

                @Override
                public void onPageFinished(WebView view, final String url) {

                }
            });

            if (!reportStatus.isEmpty()) {
                String reportPath = caseList.get(position).getReportPath();
                String url = "http://docs.google.com/gview?embedded=true&url=" + reportPath;
                String doc = "<iframe src='" + url + "' width='100%' height='100%' style='border: none;'></iframe>";
                if (!reportPath.isEmpty()) {
                    mWebView.loadData(doc, "text/html", "UTF-8");
                }
            }
        }

2 个答案:

答案 0 :(得分:1)

您可以尝试以下代码,

String url = null;
        try {
            url = URLEncoder.encode("file url", "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String path = "http://docs.google.com/gview?embedded=true&url=" + url;
        mWebView.loadUrl(path);

答案 1 :(得分:0)

将Google文档网址从https

更改为http
private void loadUrl() {
        String reportStatus = caseList.get(position).getReportStatus();
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, final String url) {

            }
        });

        if (!reportStatus.isEmpty()) {
            String reportPath = caseList.get(position).getReportPath();
            String url = "https://docs.google.com/gview?embedded=true&url=" + reportPath;
            String doc = "<iframe src='" + url + "' width='100%' height='100%' style='border: none;'></iframe>";
            if (!reportPath.isEmpty()) {
                mWebView.loadData(doc, "text/html", "UTF-8");
            }
        }
    }