Attempt to read from field 'int android.graphics.Bitmap$Config.nativeInt' on a null

国内源搜不到相关答案,特此分享一波。
场景 : 网络请求图片,拿到图片转换成Bitmap,将图片onDraw到View上,线上反馈除了这个异常。特此记录

问题分析:

  1. android aapt针对色值数量小于256个的资源图片,在 aaptOptins cruncherEnabled为true时进行了png压缩操作。将png图片的32位全彩色模式图片转换成了8bit位索引模式

  2. android较低版本系统在使用PreConfig=565模式下解析8bit索引模式图片时能够正常返回解析后的Bitmap,但Bitmap Config为空。导致后续的加载错误。
    根据线上反馈的情况来看,都是 Android 7 以下的版本。

  3. 为什么Bitmap.getConfig()返回null?
    如果这个值并未在 Java 层 Bitmap.Config 中公开,就返回 null,像索引颜色对应的 kIndex8_Config 就会导致 getConfig() 会返回 null。

image.png
image.png

如上所示,sConfigs获取不到就返回null 。
解决方案
bitmap.copy(bitmap.config, bitmap.isMutable) ----> bitmap.copy(Bitmap.Config.ARGB_8888, bitmap.isMutable)

你可能感兴趣的:(Attempt to read from field 'int android.graphics.Bitmap$Config.nativeInt' on a null)