in my app i change the resource of an ImageView a lot of times with this method:
private void setIcon(int id, int imageSource) {
((ImageView)findViewById(id)).setBackgroundResource(imageSource);
}
After a while i get an OutOfMemoryException. Is there a better method to free the old resource (image) before i load the new one?
EDIT
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);
}