简体   繁体   English

如何清除Android中的位图内存?

[英]How to Clear Bitmap Memory in Android?

I created a Bitmap and use it in Canvas to draw a circle and this method is called every time the Activity gets called and Activity get called multi-times so that time I am getting some error like 1536000-byte external allocation too large for this process I know that error is Memory issue but how to Clear bitmap and also use at second time Activity call.. 我创建了一个位图,并在Canvas中使用它绘制了一个圆,每次调用Activity时都会调用此方法,多次调用Activity时会调用此方法,这样我会遇到一些错误,例如1536000-byte external allocation too large for this process我知道该错误是内存问题,但如何清除位图并在第二次使用Activity时也使用。

My Code is : 我的代码是:

private void Draw_Hold_Circle() {
        Bitmap bitmap_hold = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
         Canvas canvas_hold = new Canvas(bitmap_hold);
        canvas_hold.drawArc(rect_open, 0, 360, false, mOutlinePaint);

    }

This Method is called 4 times and the MainActivity can call multi-time so How to maintain Bitmap and its Memory? 该方法被调用4次,MainActivity可以被调用多次,那么如何维护位图及其内存?

Is there any special reason why you want to mantain the image in memory? 您是否有任何特殊原因要保留内存中的图像? If I'm not wrong you call your method "everytime your activity is called" so I'm guessing you mean everytime it's started with startActivity(). 如果我没记错的话,那么就将您的方法称为“每次调用您的活动”,因此我想您的意思是每次以startActivity()开始时。 If this is the case then there is no point on maintaning all the images in memory at once since the user will be able to see the one on your foreground activity. 如果是这种情况,那么就没有必要一次维护内存中的所有图像,因为用户将能够在前景活动中看到其中的一个。 I would suggest you to save your image to a file on your onStop() method and rebuild it, if necessary, on your onResume() method. 我建议您将图像保存到onStop()方法上的文件中,并在必要时在onResume()方法上对其进行重建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM