Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 534
breakline Avatar asked Oct 23 '17 10:10

breakline


2 Answers

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);
        }
    });
like image 182
Boy Avatar answered Nov 20 '22 12:11

Boy


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.

like image 31
breakline Avatar answered Nov 20 '22 12:11

breakline