Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: webview shows blank screen some times while opening pdf in google doc

Tags:

android

I am using webview in a fragment to view online PDF using http://docs.google.com/viewerng/viewer?embedded=true&url. The PDF file has many pictures and chart. It load the PDF perfectly. But some times it shows blank screen. I added the below code.

webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);

After adding this, I am unable to recreate the issue. But the above two lines are not supported in playstore. I have tried all the Links to this issue. I mostly see the blank screen in the devices with API>23.

@kkarakk Sorry for late response..Please find my code below

 webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setSupportZoom(true);
     /*     webView.getSettings().setAllowFileAccessFromFileURLs(true);
            webView.getSettings().setAllowUniversalAccessFromFileURLs(true);*/
            webView.setWebViewClient(new MyWebViewClient());
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("http://docs.google.com/gview?embedded=true&url=MYURL");

 private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            webView.loadUrl("javascript:(function() { document.querySelector('[role=\"toolbar\"]').remove();})()");
        }
    }
like image 847
Yamuna Avatar asked Jan 19 '19 06:01

Yamuna


1 Answers

Similar to Sonny's Answer but I found comparing if the title was there was more reliable. I also put an attempt count in incase it was just the url erroring so it wasn't attempting forever.

      public void onPageFinished(WebView view, String url) {
            if (view.getTitle().equals(""))
                view.loadUrl(url)
            } 
            else {
               //Success
            }
      }
like image 103
Joseph Moore Avatar answered Nov 03 '22 22:11

Joseph Moore