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)