Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chromium webview does not seems to work with Android applyOverrideConfiguration

Following my change describe in Force locale for Android flavor with resConfig I am facing a problem with webviews containing video. The issue is only on API21+ and really disappear when removing the call to applyOverrideConfiguration. Not so sure how to get around that.

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:1172)
  at org.chromium.android_webview.DefaultVideoPosterRequestHandler$1.run(DefaultVideoPosterRequestHandler.java:39)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:135)
  at android.app.ActivityThread.main(ActivityThread.java:5221)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

From what I could find on grepcode it would be while getting the ic_media_video_poster image.. I validated that this image is really in the sdk. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.1_r1/com/android/webview/chromium/WebViewContentsClientAdapter.java#WebViewContentsClientAdapter.getDefaultVideoPoster%28%29

public Bitmap More ...getDefaultVideoPoster() {
     TraceEvent.begin();
     Bitmap result = null;
     if (mWebChromeClient != null) {
         if (TRACE) Log.d(TAG, "getDefaultVideoPoster");
         result = mWebChromeClient.getDefaultVideoPoster();
     }
     if (result == null) {
         // The ic_media_video_poster icon is transparent so we need to draw it on a gray
         // background.
         Bitmap poster = BitmapFactory.decodeResource(
                 mWebView.getContext().getResources(),
                 R.drawable.ic_media_video_poster);
         result = Bitmap.createBitmap(poster.getWidth(), poster.getHeight(), poster.getConfig());
         result.eraseColor(Color.GRAY);
         Canvas canvas = new Canvas(result);
         canvas.drawBitmap(poster, 0f, 0f, null);
     }
     TraceEvent.end();
     return result;
 }

EDIT: After a few more test, I was able to isolate the crash in a testApp. It is available in the bugreport I created on Chromium https://code.google.com/p/chromium/issues/detail?id=521753

Any ideas? As anyone faced this problem already?

like image 769
Eric Labelle Avatar asked Aug 17 '15 12:08

Eric Labelle


People also ask

Does Android WebView use Chromium?

Android WebView is one of the six supported platforms for Chromium.

Is WebView based on Chromium?

Our in-app browser for Facebook on Android has historically relied on an Android System WebView based on Chromium, the open source project that powers many browsers on Android and other operating systems.

What engine does Android WebView use?

Since Android 4.4 (KitKat), the WebView component is based on the Chromium open source project. WebViews now include an updated version of the V8 JavaScript engine and support for modern web standards previously missing in old WebViews.


2 Answers

As @Martin Edlman commented, it should work with this workaround:

In Kotlin:

override fun getAssets(): AssetManager {
    return resources.assets
}

In Java:

@Override
public AssetManager getAssets() {
    return getResources().getAssets();
} 
like image 53
Martin L. Jensen Avatar answered Oct 19 '22 06:10

Martin L. Jensen


For future; You can try my own implementation. Add below code to your CustomChromeClient;

@Nullable
@Override
public Bitmap getDefaultVideoPoster() {
    if (super.getDefaultVideoPoster() == null) {
        return BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_launcher);
    } else {
        return super.getDefaultVideoPoster();
    }
}
like image 40
Ozan Avatar answered Oct 19 '22 05:10

Ozan