How can I completely clear a bitmap from memory on Android?

311 Views Asked by At

I just have a bitmap that I use to assign to a static variable. I did not set this to any imageview. After assigning it to a static variable, I want to delete it from memory by typing bitmap.recycle (). I don't get an error when I just use the bitmap.recycle () line, but when I try to switch to a different page, I get an error.

This code has no errors:

StaticVeriables.getScannedFromGallery=bitmap;
bitmap.recycle();
//Intent gallery1 = new Intent(MainActivity.this, EditImage.class);
//gallery1.putExtra("isGallery",true);
//startActivity(gallery1);
//finish();

There is an error in this code:

StaticVeriables.getScannedFromGallery=bitmap;
bitmap.recycle();
Intent gallery1 = new Intent(MainActivity.this, EditImage.class);
gallery1.putExtra("isGallery",true);
startActivity(gallery1);
finish();
1

There are 1 best solutions below

0
On BEST ANSWER

I solved this problem. It wasn't about switching to another activity. Since I assigned the bitmap to my static variable in the previous line, when I wrote bitmap.recycle (), I got an error because this bitmap is a reference static variable and I used this static variable in other classes. I solved this problem by copying my bitmap variable to my static variable. In that:

//I solved my problem with this line
StaticVeriables.getScannedFromGallery=bitmap.copy(bitmap.getConfig(),true);
/*Whe should not do this
StaticVeriables.getScannedFromGallery=bitmap;*/
bitmap.recycle();
Intent gallery1 = new Intent(MainActivity.this, EditImage.class);
gallery1.putExtra("isGallery",true);
startActivity(gallery1);
finish();