处理大位图和android活动生命周期的内存不足错误

jfi*_*isk 5 android memory-management bitmap

我有一个可滚动的地图应用程序,现在有一个巨大的位图.它在启动时加载正常,但是当它失去前台状态并且用户再次将其恢复时我得到内存不足错误.在onPause中,它使用recycle来破坏位图,并将其标记为null.onResume检查map == null是否会再次加载位图,尽管我回收了位图,但这会使程序崩溃...这里有一些代码.所有对Bitmap map的其他引用首先在加载/绘制之前检查它是否为null.

在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");
}
}
Run Code Online (Sandbox Code Playgroud)

我的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");
}
Run Code Online (Sandbox Code Playgroud)

Cri*_*ian 0

试试这样:

protected void onResume(){
    super.onResume();
    if (map == null){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTempStorage = new byte[16*1024];

        map = BitmapFactory.decodeResource(getResources(),
                        R.drawable.lowresbusmap, options);
    }
}
Run Code Online (Sandbox Code Playgroud)