如何在Android上彻底清除位图(bitmap)在内存中的数据?

huangapple go评论73阅读模式
英文:

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

问题

这段代码中,我只是有一个位图(bitmap),我用它来赋值给一个静态变量。我没有将其设置为任何ImageView。在将其赋值给静态变量之后,我想通过键入bitmap.recycle()来从内存中删除它。当我只使用bitmap.recycle()这一行时,我不会得到错误,但是当我尝试切换到另一页时,会出现错误。

这段代码没有错误:

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

这段代码中有一个错误:

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

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

得分: 0

我解决了这个问题。这与切换到另一个活动无关。由于我在前一行将位图分配给了我的静态变量,所以当我写入bitmap.recycle()时,我得到了一个错误,因为这个位图是一个引用静态变量,并且我在其他类中使用了这个静态变量。我通过将我的位图变量复制到静态变量中来解决了这个问题。在那之中:

//我用这行代码解决了我的问题
StaticVeriables.getScannedFromGallery = bitmap.copy(bitmap.getConfig(), true);
/*我们不应该这样做
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();

huangapple
  • 本文由 发表于 2020年9月29日 16:48:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64116048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定