如何在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();

我解决了这个问题。这并不是要切换到另一项活动。由于我在前一行中将位图分配给了我的静态变量,所以在编写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();

最新更新