Recycling bitmaps

旧时模样 提交于 2019-12-10 09:23:20

问题


I am working on project which involves operations on bitmaps. Because I don't want to have OutofMemoryError I'm trying to recycle each of unused bitmap. Unfortunatelly I've recycled too much and have 'trying to use a recycled bitmap' error.

I am using:

 Bitmap.createBitmap(bmp, x,y,w,h, matrix, false);
 Bitmap.createScaledBitmap(bmp, w, h,true);

Should I recycle bmp after this methods or it is recycled by them? Like:

Bitmap newBitmap = Bitmap.createBitmap(bmp, x,y,w,h, matrix, false);
bmp.recycle();

Can I just after imageView.setImageBitmap() recycle one which previously was used here? E.g.

myImageView.setImageBitmap(myBitmap);
myImageView.setImageBitmap(newBitmap);
myBitmap.recycle();

Thank you for help.


回答1:


You should only recycle a bitmap when you do not need it anymore. However, you do need a bitmap when you want to display it. If you don't want to display it, then you can recycle a bitmap.




回答2:


You only recycle bitmaps once you are done with them and are sure you never need to use the data in them again. It's not a magic method that you can use anywhere you like to give you more memory when dealing with bitmaps.




回答3:


When I want to scale bitmap then I use the same reference:

Bitmap bmp = Bitmap.createBitmap(bmp, x,y,w,h, matrix, false); 

When the first bitmap object loses its reference "bmp" it gets removed by GC (garbage collector) so you don't need to recyle anything. As for the image view it only references its source to "newBitmap" it does not create its own.



来源:https://stackoverflow.com/questions/11418354/recycling-bitmaps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!