2010-07-24 51 views
5

我有一個可滾動的地圖應用程序,它現在有一個巨大的位圖。它在啓動時加載得很好,但是當它丟失前景狀態並且用戶再次將它回退時,出現內存不足錯誤。在onPause中,它使用回收來刪除位圖,並將其標記爲空。 onResume會檢查map == null是否會再次加載位圖,儘管我回收了位圖,但程序崩潰了......以下是一些代碼。在加載/繪圖之前,首先檢查位圖貼圖的所有其他引用是否爲空。內存不足錯誤處理大型位圖和Android活動生命週期

的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"); 
} 
} 

我的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"); 
} 
+0

你在調試器中收到此錯誤,或者真正的手機上? – EboMike 2010-07-24 23:14:01

+0

真正的手機(摩托羅拉機器人) – jfisk 2010-07-25 17:12:27

回答

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); 
    } 
} 
+0

沒有運氣,這個圖像實際上是爲了更快的手機。像G1這樣的較慢的將使用較小的位圖。在這種情況下,我將16 * 1024更改爲24 * 1024?不幸的是,代碼仍然給我同樣的錯誤。 – jfisk 2010-07-24 16:57:37