如何从 Android 上的内存中完全清除位图?

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

我只有一个用于分配给静态变量的位图。我没有将其设置为任何图像视图。将其分配给静态变量后,我想通过键入 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();

我解决了这个问题。这不是关于切换到另一个 activity。由于我在上一行将位图赋值给了我的静态变量,所以当我写bitmap.recycle()时,报错了,因为这个位图是一个引用静态变量,而我在其他类中使用了这个静态变量.我通过将我的位图变量复制到我的静态变量来解决这个问题。其中:

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