breakline
breakline

Reputation: 6083

Webview crashes when displaying a youtube video

After updating to 8.0 we get a crash so far we haven't seen before:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
                                                                 at com.android.webview.chromium.WebViewContentsClientAdapter.getDefaultVideoPoster(WebViewContentsClientAdapter.java:536)
                                                                 at org.chromium.android_webview.DefaultVideoPosterRequestHandler$1.run(DefaultVideoPosterRequestHandler.java:2)
                                                                 at android.os.Handler.handleCallback(Handler.java:789)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                 at android.os.Looper.loop(Looper.java:164)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Is this a bug in Chrome?

Upvotes: 7

Views: 2486

Answers (2)

Boy
Boy

Reputation: 7499

Thanks to @breakline's answer, I got this issue worked around! Thanks! But instead of using decoding a bitmap, I just create an empty one and return that:

    setWebChromeClient(new WebChromeClient() {
        @Override
        public Bitmap getDefaultVideoPoster() {
            return Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
        }
    });

Upvotes: 17

breakline
breakline

Reputation: 6083

Anyone still wondering instead of downvoting here is the actual fix to this. Since I spent quite some time on this I will post it here.

You need to add the following to your WebChromeClient:

webview.setWebChromeClient(new WebChromeClient() {
        @Override
        public Bitmap getDefaultVideoPoster() {
            Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.black);
            return icon;
        }
    });

Any drawable is fine as long as it is an actual PNG file. If you use a drawable/xml you still get an exception.

Upvotes: 11

Related Questions