如果有大尺寸的图片加载到界面中,而且没有及时回收资源的话,很有可能出现OutOfMemoryError,导致app崩溃。
怎么回收图片资源呢?方法是在onDestroy中通过Bitmap对象来释放资源:
if (imgBg != null) { // imgBg为ImageView对象
Bitmap bitmap = ((BitmapDrawable) imgBg.getDrawable()).getBitmap();
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
bitmap = null;
imgBg = null;
}
}
System.gc();