您的位置:首页 > 其它

Binary XML file line #4: Error inflating class 及OOM异常

2016-12-30 14:31 281 查看

Binary XML file line #4: Error inflating class 及OOM异常

程序可以编译通过并运行,但是在部分机型上出现 Binary XML file line #4: Error inflating class 及java.lang.OutOfMemoryError: Failed to allocate a 74649612 byte allocation with 4194208 free bytes and 9MB until OOM异常。

解决方法:

不要在XML文件中设置图片,如android:background=”@drawable/bg”,android:src=”@drawable/bg”,需要在Java代码中设置。

public BitmapDrawable getdrawable(int res){
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
//获取资源图片
InputStream is = this.getResources().openRawResource(res);
Bitmap bitmap = BitmapFactory.decodeStream(is,null, opt);
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return new BitmapDrawable(this.getResources(),bitmap);
}

ImageView iv=(ImageView) view.findViewById(R.id.iv);
iv.setImageDrawable(getdrawable(R.drawable.bg));


至于相关原理请自行百度。

http://jingyan.baidu.com/article/ed2a5d1f3191d109f6be17e8.html

http://www.eoeandroid.com/thread-326343-1-1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐