1

Webview is not loading a PDF from url.I am getting Preview not Available.

Please any one help on this thanks.,

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");
                }
            }
        }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Shanmuga M
  • 71
  • 1
  • 5

2 Answers2

3

Hi try the below code,

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);
Shanmugapriyan M
  • 189
  • 1
  • 2
  • 12
0

Change the google docs url to https from 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");
            }
        }
    }
Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74