如何清除Android中的缓存图片?
如何在Android中以编程方式清除内存中的缓存图像?
当我滚动它重新加载图像时,我有一个带有图标的 ListView
。因此它会产生OutofMemoryError
。我想在收到此异常时清除缓存。怎么办?有什么帮助吗?
编辑:
我只是在我的程序中使用此代码来加载图像: http ://ballardhack.wordpress.com/2010/04/10/loading-images-over-http-on-a-separate-thread-on-android/
How to clear the cached image from memory programatically in Android?
I have a ListView
with icons when I scroll its reloads the image. So its produce the OutofMemoryError
. I want clear the cache while gets this exception. how to do that? any help?
EDIT:
i am just using this code on my program to loadimage:
http://ballardhack.wordpress.com/2010/04/10/loading-images-over-http-on-a-separate-thread-on-android/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否在 ListView 中重复使用位图对象?
Romain Guy 在他的关于布局和视图的 Android 演讲中谈到了这对于内存和流畅性能的重要性< /a> 去年的 Google I/O。
本质上,你应该有一定数量的位图对象(他使用了 8 个),并且每次你在滚动时加载下一张图像时,它都应该进入刚刚消失的图像的对象中。
您可能认为缓存图像速度更快,但它会导致内存问题和垃圾收集问题,从而不可避免地导致延迟。
Are you re-using the bitmap objects in the ListView?
Romain Guy talked about how important this is for memory and smooth performance in his Android talk on layouts and views at Google I/O last year.
Essentially, you should have a certain number of bitmap objects (he used 8) and every time you load the next image as you scroll, it should go into the object of the one that just disappeared.
You might think caching the images is faster, but it causes memory problems and garbage collecting issues which inevitably causes lag.
是的...已知的“问题”,假设这是 ListView 的行为。
如何解决此问题:
按照 @HXCaine 的建议观看视频的前 15 分钟,其中解释了 ViewHolder。
如果我没记错的话,您的示例应该在位图为空时设置默认图像!在示例中,您不将其提供给视图,因此它会被缓存。 Shur 这应该由框架处理,但事实并非如此:(。
示例代码:
我希望它有帮助。
Yup... known "problem", let say this is the behaviour of the ListView.
How to fix it:
Watch the first 15min of the video as suggested by @HXCaine, which explains the ViewHolder.
If I'm not mistaken your example should set the default image when a bitmap is null! In the example you do not provide this to a view and so it gets cached. Shur this should be handled by the framework but it is not :(.
Example code:
I hope it helps.
您可以通过调用 回收方法< /a> 如果您使用位图。但是,我不太确定这是否能解决您的问题。
You can free up some memory by calling the recycle method if you're using Bitmap. However, I'm not really sure if this will solve your problem.