【发布时间】:2013-12-21 00:37:16
【问题描述】:
在我的应用程序中,我使用此方法多次更改 ImageView 的资源:
private void setIcon(int id, int imageSource) {
((ImageView)findViewById(id)).setBackgroundResource(imageSource);
}
过了一会儿,我得到了 OutOfMemoryException。在我加载新资源(图像)之前,有没有更好的方法来释放旧资源(图像)?
编辑
private void setIcon(int id, int imageSource) {
ImageView imageView = (ImageView)findViewById(id);
Drawable bg= imageView.getBackground();
if(bg != null && bg instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) bg;
bitmapDrawable.getBitmap().recycle();
if(D)Log.d(TAG, "recycled bitmap");
}
Bitmap bm = BitmapFactory.decodeResource(getResources(), imageSource);
imageView.setImageBitmap(bm);
}
【问题讨论】:
-
检查这个答案:stackoverflow.com/questions/13118005/… 并检查 cmets。
标签: android exception resources imageview out-of-memory