Chromium webview does not seems to work with Android applyOverrideConfiguration

一笑奈何 提交于 2019-12-09 14:51:53

问题


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?


回答1:


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();
    }
}



回答2:


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();
} 


来源:https://stackoverflow.com/questions/32050784/chromium-webview-does-not-seems-to-work-with-android-applyoverrideconfiguration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!