ANDROID:帮助,outofmemory位图超出预算大小。如何回收?

时间:2011-08-12 14:33:32

标签: android exception out-of-memory

我用我的Android应用程序碰壁了,因为我的OutOfMemoryException描述了我的“位图超过VM预算”等等。奇怪的是,代码在一台PC上工作,但在另一台PC上触发了这个异常。

我喜欢10张900x500图像,我正在使用的代码如下。我试过recycle(),这使得错误和图像都不可见。我真的需要帮助。在此先感谢:)

public class AVMSystemActivity extends Activity implements Runnable{
    /** Called when the activity is first created. */
    Field[] drawables;
    ImageView imageView;
    boolean adFlip=true;
    Resources a;
    int k=0x7f020000;
    int last;
    private RefreshHandler mRedrawHandler = new RefreshHandler();  
    int adCount=0;
    RelativeLayout ll;
    class RefreshHandler extends Handler {  
        @Override  
        public void handleMessage(Message msg) {  
            AVMSystemActivity.this.updateAd();  
        }  

        public void sleep(long delayMillis) {  
            this.removeMessages(0);  
            sendMessageDelayed(obtainMessage(0), delayMillis);  
        }  
    };  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i("TEST","A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        drawables = sepekin.software.R.drawable.class.getFields();

        a=this.getResources();
        int i=0x7f020000;
        boolean done=true;
        ll = (RelativeLayout) findViewById(R.id.rlout);
        imageView = new ImageView(this);
        ll.addView(imageView);

        try
        {
            for (i=0x7f020000;i<0x7f020040;i++)
            {
                String name=a.getResourceName(i);
                name=name.substring(name.indexOf(':')+1);
                name=name.substring(9);
                if(name.startsWith("ad"))
                {
                    Log.i("res tara",name);
                    //imageView.setImageDrawable(a.getDrawable(i));//------
                    //imageView.setBackgroundDrawable(a.getDrawable(i));            
                    adCount++;
                }
                else
                {
                    last=i;
                    Log.i("LAST ELSE",i+"");
                    break;
                }
            }
        }
        catch(Exception e)
        {
            last=i;
            Log.i("LAST",i+"");
            done=false;
            e.printStackTrace();
        }
        Button btn=(Button)findViewById(R.id.restbutton);
        btn.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) { 
                Log.i("ACTION","BUTTON CLICKED");
                adFlip=!adFlip;
                try
                {
                    Intent i = new Intent(AVMSystemActivity.this,RestListActivity.class);   
                    startActivity(i);
                }
                catch(Exception e)
                {
                    Log.i("cort",e.toString());
                }
            } 
        });
        updateAd();
    }
    int currentInt=0;

    private void updateAd(){  
        Log.i("update","lan");
        try
        { 
            if(adFlip)
            {  
                mRedrawHandler.sleep(1000);
                //*******
                if(k==last)
                    k=0x7f020000;
                else 
                {
                    if(a.getResourceName(k).contains("icon"))
                    {
                        Log.i("update",a.getResourceName(k));
                    }
                    else
                    {
                        Log.i("update",a.getResourceName(k)+"__--");

                        imageView = new ImageView(this);
                        imageView.setImageDrawable(a.getDrawable(k));
                        imageView.setPadding(20, 0, 0, 0);
                        ll.addView(imageView);
                        //imageView.getDrawingCache().recycle();
                    }
                    k++;
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }  
    @Override
    public void run() {
        // TODO Auto-generated method stub
    }
}

1 个答案:

答案 0 :(得分:0)

在将Bitmap添加到ImageView后,应该在位图上调用recycle()。适合我。