android: Bitmap size exceeds VM budget

Here's a drop-in solution if anyone is interested. Just derive from BetterActivity instead of Activity. This will clean up all the drawables on orientation change, and likely on activity switches etc...

public abstract class BetterActivity extends Activity
{
  @Override
  protected void onResume()
  {
    System.gc();
    super.onResume();
  }
  
  @Override
  protected void onPause()
  {
    super.onPause();
    System.gc();
  }
  
  @Override
  public void setContentView(int layoutResID)
  {
    ViewGroup mainView = (ViewGroup)
      LayoutInflater.from(this).inflate(layoutResID, null);
    
    setContentView(mainView);
  }
  
  @Override
  public void setContentView(View view)
  {
    super.setContentView(view);
    
    m_contentView = (ViewGroup)view;
  }
  
  @Override
  public void setContentView(View view, LayoutParams params)
  {
    super.setContentView(view, params);
    
    m_contentView = (ViewGroup)view;
  }
  
  @Override
  protected void onDestroy()
  {
    super.onDestroy();
    
    // Fixes android memory  issue 8488 :
    // http://code.google.com/p/android/issues/detail?id=8488
    nullViewDrawablesRecursive(m_contentView);
    
    m_contentView = null;
    System.gc();
  }
  
  private void nullViewDrawablesRecursive(View view)
  {
    if(view != null)
    {
      try
      {
        ViewGroup viewGroup = (ViewGroup)view;
        
        int childCount = viewGroup.getChildCount();
        for(int index = 0; index < childCount; index++)
        {
          View child = viewGroup.getChildAt(index);
          nullViewDrawablesRecursive(child);
        }
      }
      catch(Exception e)
      {          
      }
      
      nullViewDrawable(view);
    }    
  }

  private void nullViewDrawable(View view)
  {
    try
    {
      view.setBackgroundDrawable(null);
    }
    catch(Exception e)
    {          
    }
    
    try
    {
      ImageView imageView = (ImageView)view;
      imageView.setImageDrawable(null);
      imageView.setBackgroundDrawable(null);
    }
    catch(Exception e)
    {          
    }
  }

  // The top level content view.
  private ViewGroup m_contentView = null;
}

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值