Akash Sethi
Akash Sethi

Reputation: 81

Android , imageview help. Is the previous picture out of memory if i replace it?

public class TestingActivity extends Activity  {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getStringIdentifier(getBaseContext(),"main","layout"));
    final ImageView iv=(ImageView) findViewById(R.id.imageView1);
    final Resources res=getResources();
    iv.setImageDrawable(res.getDrawable(R.drawable.horse));
    Button b1=(Button) findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            iv.setImageDrawable(res.getDrawable(R.drawable.labrador));
        }
    });
}
public static int getStringIdentifier(Context context, String name,String resource) {
    return context.getResources().getIdentifier(name, resource, context.getPackageName());
}

}

im trying to figure out something. After i click the button, the horse is replaced by the labrador , my question is, is the picture of the horse still in memory? and if i put it back there again will there be two instances of the same picture?

Upvotes: 0

Views: 510

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

you can try to retrieve the ImageView content before set a new bitmap:

BitmapDrawable content = (BitmapDrawable)iv.getDrawable();
if (content != null) {
     Bitmap contentBitmap = content.getBitmap();
     if (contentBitmap != null) {
           contentBitmap.recycle();
           contentBitmap = null;
     }
}

Upvotes: 1

Related Questions