Bitmap没有及时释放的话经常会出现OOM错误。由于Bitmap占用的是底层C的内存,JVM的垃圾回收机制对他没有用。用完后必须显示的调用 recycle();告诉虚拟器该Bitmap没有用了,可以释放了,能后虚拟器才能在稍后的时候释放。
1.Bitmap变量
Bitmap bit = BitmapFactory.decodeFile(path);
if(bit != null && !bit.isRecycled()) {
bit.recycle();
}2.Drawable
先把Drawable转成 BitmapDrawable 在释放
BitmapDrawable bd = (BitmapDrawable)d;
if(!bd.getBitmap().isRecycled()) {
bd.getBitmap().recycle();
}3.public void drawBitmap(int[] colors, int offset, int stride, float x,
float y, int width, int height, boolean hasAlpha,
Paint paint)
本文详细介绍了如何有效管理Android中Bitmap的内存使用,防止出现OOM错误。包括Bitmap变量、Drawable及BitmapDrawable的正确释放方法。

5万+

被折叠的 条评论
为什么被折叠?



