2012-04-24 65 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));