首页 > 代码库 > <bitmap> requires a valid src attribute
<bitmap> requires a valid src attribute
E/AndroidRuntime( 1601): FATAL EXCEPTION: main
E/AndroidRuntime( 1601): Process: com.android.camera2, PID: 1601
E/AndroidRuntime( 1601): android.content.res.Resources$NotFoundException: File res/drawable/ic_capture_video.xml from drawable resource ID #0x7f02004b
E/AndroidRuntime( 1601): at android.content.res.Resources.loadDrawableForCookie(Resources.java:2428)
E/AndroidRuntime( 1601): at android.content.res.Resources.loadDrawable(Resources.java:2330)
E/AndroidRuntime( 1601): at android.content.res.Resources.getDrawable(Resources.java:758)
E/AndroidRuntime( 1601): at android.content.res.Resources.getDrawable(Resources.java:724)
E/AndroidRuntime( 1601): at com.android.camera.ui.BottomBar.setShutterButtonIcon(BottomBar.java:432)
E/AndroidRuntime( 1601): at com.android.camera.app.CameraAppUI.setBottomBarShutterIcon(CameraAppUI.java:1706)
E/AndroidRuntime( 1601): at com.android.camera.app.CameraAppUI.resetBottomControls(CameraAppUI.java:1647)
E/AndroidRuntime( 1601): at com.android.camera.CameraActivity.onModeSelected(CameraActivity.java:2127)
E/AndroidRuntime( 1601): at com.android.camera.app.CameraAppUI.onModeSelected(CameraAppUI.java:1414)
E/AndroidRuntime( 1601): at com.android.camera.ui.ModeListView.onModeSelected(ModeListView.java:1238)
E/AndroidRuntime( 1601): at com.android.camera.ui.ModeListView.access$4600(ModeListView.java:64)
E/AndroidRuntime( 1601): at com.android.camera.ui.ModeListView$PeepholeAnimationEffect$8$1.run(ModeListView.java:2120)
E/AndroidRuntime( 1601): at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime( 1601): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime( 1601): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime( 1601): at android.app.ActivityThread.main(ActivityThread.java:5221)
E/AndroidRuntime( 1601): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 1601): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 1601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
E/AndroidRuntime( 1601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
E/AndroidRuntime( 1601): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #25: <bitmap> requires a valid src attribute
E/AndroidRuntime( 1601): at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:744)
E/AndroidRuntime( 1601): at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:709)
E/AndroidRuntime( 1601): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1095)
E/AndroidRuntime( 1601): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:185)
E/AndroidRuntime( 1601): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1095)
E/AndroidRuntime( 1601): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1017)
E/AndroidRuntime( 1601): at android.content.res.Resources.loadDrawableForCookie(Resources.java:2418)
E/AndroidRuntime( 1601): Process: com.android.camera2, PID: 1601
E/AndroidRuntime( 1601): android.content.res.Resources$NotFoundException: File res/drawable/ic_capture_video.xml from drawable resource ID #0x7f02004b
E/AndroidRuntime( 1601): at android.content.res.Resources.loadDrawableForCookie(Resources.java:2428)
E/AndroidRuntime( 1601): at android.content.res.Resources.loadDrawable(Resources.java:2330)
E/AndroidRuntime( 1601): at android.content.res.Resources.getDrawable(Resources.java:758)
E/AndroidRuntime( 1601): at android.content.res.Resources.getDrawable(Resources.java:724)
E/AndroidRuntime( 1601): at com.android.camera.ui.BottomBar.setShutterButtonIcon(BottomBar.java:432)
E/AndroidRuntime( 1601): at com.android.camera.app.CameraAppUI.setBottomBarShutterIcon(CameraAppUI.java:1706)
E/AndroidRuntime( 1601): at com.android.camera.app.CameraAppUI.resetBottomControls(CameraAppUI.java:1647)
E/AndroidRuntime( 1601): at com.android.camera.CameraActivity.onModeSelected(CameraActivity.java:2127)
E/AndroidRuntime( 1601): at com.android.camera.app.CameraAppUI.onModeSelected(CameraAppUI.java:1414)
E/AndroidRuntime( 1601): at com.android.camera.ui.ModeListView.onModeSelected(ModeListView.java:1238)
E/AndroidRuntime( 1601): at com.android.camera.ui.ModeListView.access$4600(ModeListView.java:64)
E/AndroidRuntime( 1601): at com.android.camera.ui.ModeListView$PeepholeAnimationEffect$8$1.run(ModeListView.java:2120)
E/AndroidRuntime( 1601): at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime( 1601): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime( 1601): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime( 1601): at android.app.ActivityThread.main(ActivityThread.java:5221)
E/AndroidRuntime( 1601): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 1601): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 1601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
E/AndroidRuntime( 1601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
E/AndroidRuntime( 1601): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #25: <bitmap> requires a valid src attribute
E/AndroidRuntime( 1601): at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:744)
E/AndroidRuntime( 1601): at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:709)
E/AndroidRuntime( 1601): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1095)
E/AndroidRuntime( 1601): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:185)
E/AndroidRuntime( 1601): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1095)
E/AndroidRuntime( 1601): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1017)
E/AndroidRuntime( 1601): at android.content.res.Resources.loadDrawableForCookie(Resources.java:2418)
E/AndroidRuntime( 1601): ... 19 more
由上述Exception 带来的收获:
这个是Andoird内置系统app Camera的代码抛出的NotFoundException异常
直接原因是:在drawable下的一个xml文件中引用的bitmap对象无效导致资源找不到。
导火索:该应用的owner在编码时不小把图片的命名和drawable下xml文件的名字相同了。正常情况下这也不是问题,android可以区分xml和png文件,关键是在某些情况下问题就来了。
事情上下文:我们公司之前的产品都是hdpi的产品,而最近的项目是小屏的ldpi的,在配置board的时候,直接从原有的board上fork过来了一个board,而PRODUCT_AAPT_CONFIG 依然是hdpi,这就导致编译出来的apk,资源都是只hdpi的。当应用在运行时加载ldpi的资源时,找不到,于是就找附件高分辨率的资源,这些刚好把drawable目录下的ic_capture_video.xml当当成了ic_captrue_video.png(都是drawable/ic_capture_video)所以引用了个无效的bitmap源。
suggestion to fix this bug:
1,Camera app应用避免出现xml和png同名以防止不测;
2,board配置事实求实,根据dip配置PRODUCT_AAPT_CONFIG
总结:这是个多个不规范并发的问题。
1, dpi与屏幕分辨率/物理尺寸的计算公式
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
2 PRODUCT_AAPT_CONFIG = xxxx对于资源选择的匹配规则?
原则上来说,优先使用符合设备dpi的资源,其次是dpi较低的高dpi资源,再次是dpi较高的高dpi资源,最后采用nodpi的资源,由此,根据设备自身的dpi的不同,不同dpi资源的优先级是有差异的(忽略mdpi&hdpi):
设备dpi | 优先级顺序(由高到低) |
tvdpi | tvdpi>hdpi>xhdpi>xxhdpi>mdpi>default>ldpi>nodpi |
hdpi | hdpi>tvdpi>xhdpi>xxhdpi>>mdpi>default>ldpi>nodpi |
xhdpi | xhdpi>xxhdpi>hdpi>tvdpi>mdpi>default>ldpi>nodpi |
xxhdpi | xxhdpi>xhdpi>hdpi>tvdpi>mdpi>default>ldpi>nodpi |
<bitmap> requires a valid src attribute
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。