ANDROID:帮助,内存不足的位图超出了预算大小。如何回收?

发布于 2024-11-29 19:47:22 字数 4163 浏览 1 评论 0原文

我的 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
    }
}

I've hit a wall with my Android-app, as I'm getting an OutOfMemoryException with the description that my "bitmap exceeds VM budget" and so on. The weird thing is that the code works on one PC but triggers this exception in another.

I have like 10 900x500 images and the code I'm using is enclosed below. I have tried to recycle(), which renders both the errors and the images invisible. I really need help with this. Thanks in advance:)

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾性傲以野 2024-12-06 19:47:22

将 Bitmap 添加到 ImageView 后,您应该在 Bitmap 上调用 recycle()。对我有用。

You should call recycle() on the Bitmap after you've added the Bitmap to your ImageView. Works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文