我有一个可滚动的地图应用程序,目前有一个巨大的位图。它在启动时加载得很好,但当它失去前台状态并且用户再次将其带回时,我会收到内存不足错误。在onPause中,它使用recycle来清除位图,并将其标记为null。 onResume检查map == null是否为真,然后再次加载位图,尽管我已经回收了位图,但这仍会导致程序崩溃...这是一些代码片段。 所有对Bitmap map的其他引用都会在加载/绘制之前首先检查它是否为空。
onPause
protected void onPause() {
super.onPause();
Log.e("sys","onPause was called");
if (map != null)
{
map.recycle();
map = null;
System.gc();
Log.e("sys","trashed the map");
}
}
my onResume
protected void onResume(){
super.onResume();
Log.e("sys","onResume was called");
if (map == null)
map = BitmapFactory.decodeResource(getResources(),
R.drawable.lowresbusmap);
Log.e("sys","redrew the map");
}