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

后端 未结 5 2094
感情败类
感情败类 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:33

    I was having the exact same issue and found that there was always a chance the WebView would not load on the first load attempt, especially if the pdf was on the larger side. The code I put together below works 100% of the time. From my beginner's understanding, it safely utilizes a separate thread to loop through and test the load status of the WebView, re-attempting a load of the view until successful. As this question was posted a year ago, I have generalized my solution to best benefit new viewers.

    public class WebViewActivity extends AppCompatActivity {
    
    String PDFView;
    WebView webView;
    String PDFBrowserView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Get the intended "PDFView"
        PDFView = getIntent().getExtras().get("PDFView").toString();
        //Have to manually encode (?) the url to display it
        try {
            PDFView = URLEncoder.encode(PDFView, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //Full display url
        PDFBrowserView = "https://docs.google.com/gview?embedded=true&url=" + PDFView;
    
        //Initialize a new "WebView" instance
        webView = new WebView(WebViewActivity.this);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        //This handles callbacks (?)
        webView.setWebChromeClient(new WebChromeClient());
    
        //Call this to load page if page is blank with pdf url until page is not blank
        checkPageFinished();
    }
    
    public void checkPageFinished() {
        //If view is blank:
        if (webView.getContentHeight() == 0) {
    
            //Run off main thread to control delay
            webView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //Load url into the "WebView"
                    webView.loadUrl(PDFBrowserView);
                }
                //Set 1s delay to give the view a longer chance to load before 
                // setting the view (or more likely to display blank)
            }, 1000);
            //Set the view with the selected pdf
            setContentView(webView);
    
            webView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //If view is still blank:
                    if (webView.getContentHeight() == 0) {
                        //Loop until it works
                        checkPageFinished();
                    }
                }
                //Safely loop this function after 1.5s delay if page is not loaded
            }, 1500);
    
            }
        }
    }
    

提交回复
热议问题