Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webview.draw(Canvas) throws exception in Android 5.0

I write the following code to generate the bitmap using webview and I got the following exception webView.draw(canvas); in Android Lolipop, it's working except the 5.0:

Code:

protected Bitmap doInBackground(Void... params) {
            try {
                Bitmap bitmap = null;
                Thread.sleep(5000);

                if (webView.getMeasuredHeight() > 0) {
                    bitmap = Bitmap.createBitmap(webView.getMeasuredWidth(),
                            webView.getMeasuredHeight(), Config.ARGB_8888);
                }
                Canvas canvas = new Canvas(bitmap);

                webView.draw(canvas);
                return bitmap;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

Exception:

java.lang.RuntimeException: Probable deadlock detected due to WebView API being called on incorrect thread while the UI thread is blocked.

How can I fix this?

like image 538
Mayank Vyas Avatar asked Feb 13 '26 07:02

Mayank Vyas


1 Answers

This exception is thrown from WebViewChromium.runBlockingFuture().

There, some task (in your case, probably the drawing) is put on a queue to run on the UI thread.

Then, we wait for the result for up to four seconds. If the task hasn't completed by that time, you'll get that exception.

I see two possible reasons for taking that long:

  1. The drawing actually takes that long for some reason (very complex page?).
  2. Your main/UI thread is actually very busy during this time, so the task doesn't actually get to run.

I think #2 is more likely. You should check what your main thread is doing at this point. Are there any potential mutex issues, like a deadlock? Or maybe it's just very busy?

like image 90
Snild Dolkow Avatar answered Feb 15 '26 21:02

Snild Dolkow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!