Mayank Vyas
Mayank Vyas

Reputation: 31

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?

Upvotes: 3

Views: 501

Answers (1)

Snild Dolkow
Snild Dolkow

Reputation: 6866

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?

Upvotes: 1

Related Questions