2012-04-24 64 views
0

我正在開發一種應用程序,使用一種本地背景。這個想法是使這個變化。我設法使用此代碼更改「背景」。無法刪除可繪製的虛擬機預算崩潰

Bitmap bMap = BitmapFactory.decodeFile(sharedPreferences.getString("PICTURE", "")); 
     Drawable d =new BitmapDrawable(bMap); 
     bac.setBackgroundDrawable(d); 
     } 

問題是,每當我返回到「背景屏幕」,應用程序崩潰,因爲OutOfMemoryError。然後它顯示新的背景。我需要一些使應用程序不會崩潰的代碼。我在ImageView中管理這個,但不在LinearLayout中。對於ImageView的我用這個代碼:

Bitmap bMap = BitmapFactory.decodeFile(sharedPreferences.getString("PICTURE", "")); 
    image.setImageBitmap(bMap); 

,並避免其崩潰:

@Override 
protected void onPause() { 
super.onPause(); 

unbindDrawables(findViewById(R.id.iv_pic)); 
System.gc(); 

} 

private void unbindDrawables(View view) { 
    if (view.getBackground() != null) { 
    view.getBackground().setCallback(null); 
    } 
    if (view instanceof ViewGroup) { 
     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 
     unbindDrawables(((ViewGroup) view).getChildAt(i)); 
     } 
    ((ViewGroup) view).removeAllViews(); 
    } 
} 
@Override 
protected void onDestroy() { 
super.onDestroy(); 

unbindDrawables(findViewById(R.id.iv_pic)); 
System.gc(); 

} 

我如何做同樣的LinearLayout中?

回答

0

有很多的問題,以便在詢問有關OOM異常,如何處理它。在發佈問題前請發送search the forum

一些注意事項:

  1. 可以釋放使用recycle方法由Bitmap消耗的內存(在你的代碼,你可以調用bMap.recycle()
  2. 可以使用findViewById檢索引用您的LinearLayout和傳遞到unbindDrawablesunbindDrawables(findViewById(R.id.myLinearLayoutId));