Android Webview cannot render the pdf sometimes and shows blank/white page instead

后端 未结 5 2130
感情败类
感情败类 2021-01-11 13:51
  • Open the pdf in the webview using google docs
  • Open the same pdf or different pdf again and again.
  • Sometimes it will show the blank/white page in the
5条回答
  •  余生分开走
    2021-01-11 14:32

    We can solve the Problem in the two ways. 1. One is to use the Js.Pdf Plugin on the server end. It surely solve the problem but if we have multiple pdf's in the Fragment then it may cause the out of memory situations and app can crash. 2. Second option is we can recursively called the function to load webview. This will also cause the issue but with less frequency Below is the code:

    private void showPdf(final String imageString) {
    pdfView.invalidate();
    pdfView.getSettings().setJavaScriptEnabled(true);
    pdfView.getSettings().setSupportZoom(true);
    pdfView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + imageString);
    pdfView.setWebViewClient(new WebViewClient() {
        boolean checkhasOnPageStarted = false;
    
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            checkhasOnPageStarted = true;
        }
    
        @Override
        public void onPageFinished(WebView view, String url) {
            if (checkhasOnPageStarted ) {
                pdfView.loadUrl(removePdfTopIcon);
            } else {
                showPdf(imageString);
            }
        }
    });
    }
    

提交回复
热议问题