代码之家  ›  专栏  ›  技术社区  ›  Shanmuga M

Webview未从URL加载PDF

  •  1
  • Shanmuga M  · 技术社区  · 6 年前

    Webview未从url加载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 回复  |  直到 6 年前
        1
  •  3
  •   Shanmugapriyan M    6 年前

    您好,请尝试以下代码,

    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);
    
        2
  •  0
  •   Naveen Kumar M    6 年前

    将google docs url更改为 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");
                }
            }
        }