Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw WebView into a Canvas in Android-L

I'm using Android Print Framework to save the HTML Content of the WebView as PDF. Since WebView in Android-L has a new approach to render the WebView content, it is advised to Call enableSlowWholeDocumentDraw() method before creating any webviews. This will disable rendering optimization so WebView will be able to render the whole HTML page.

Here is the part of the Code to Draw WebView into a Canvas.

PdfDocument.Page page = document.startPage(pageInfo);
mWebView.draw(page.getCanvas());

I tried calling WebView.enableSlowWholeDocumentDraw() from Application, BaseActivity and also right after and before Creating WebView inside my Fragment. But this didn't help.

I tried enabling/disabling Hardware Acceleration by setting the Layer Type as SOFTWARE and HARDWARE but this didn't help as well. I still cannot draw the whole WebView. Only the Visible portion will be drawn to the Canvas.

Has anybody noticed this behaviour ? Is there a solution for that or is this a Bug ?

EDIT

It turns out that it's not about WebView.enableSlowWholeDocumentDraw() and we are still discussing about the issue. This is the Bug Report. And here is the Sample App to reproduce the issue.

like image 948
osayilgan Avatar asked May 13 '15 18:05

osayilgan


People also ask

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

How do I import WebView?

Modify src/MainActivity. java file to add WebView code. Run the application and choose a running android device and install the application on it and verify the results. Following is the content of the modified main activity file src/MainActivity.

Is Android WebView deprecated?

Beginning October 5, 2021, Facebook Login will no longer support using Android embedded browsers (WebViews) for logging in users.


2 Answers

I am the author of this question.

To fix this problem, I had to call WebView.enableSlowWholeDocumentDraw() before inflating the view in your fragment. In the comments of Mikhail Naganov's answer, I saw that you tried a lot of options but not this one. Below is the code that I use in the onCreateView() method of your fragment with the WebView.

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle){
    //Enable the drawing of the whole document for Lollipop to get the whole WebView
    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
        WebView.enableSlowWholeDocumentDraw();
    }
    View view = inflater.inflate(R.layout.my_layout, parent, false);

   /* Rest of code */
}

There's really nothing else to it. If that doesn't work, then it might be a bug and you should report as Mikhail had suggested.

Hope this helps !

like image 196
jguerinet Avatar answered Oct 25 '22 08:10

jguerinet


Struggled with this/similar issue myself for ages. Finally found a solution, for some reason webView.draw(page.getCanvas()) renders the whole content with KitKat but not with Lollipop (even with enableSlowWholeDocumentDraw()).
However, using (webView.capturePicture()).draw(page.getCanvas()) in combination with enableSlowWholeDocumentDraw() DOES work for Lollipop, although capturePicture() is deprecated.
A final potentially important point to note is that typically you see many calls to "onDraw" as a webView loads its data and I found that with Lollipop my snapshot was getting taken prematurely (before the content had been fully loaded - annoyingly even the onPageFinished() callback method of webViewClient does not guarantee this!) - my solution was to have a custom webView and in the onDraw method, add this as the FIRST LINE: if(this.getContentHeight() == 0) return; i.e. don't bother to do any drawing unless there is some proper content. That way you should never get a blank page snapshot - although from your description it sounds like this might not be impacting you....

like image 34
hmac Avatar answered Oct 25 '22 10:10

hmac