简体   繁体   中英

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

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();

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM