如何清除Android中的缓存图片?

发布于 2024-09-04 01:53:30 字数 438 浏览 7 评论 0原文

如何在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 技术交流群。

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

发布评论

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

评论(3

子栖 2024-09-11 01:53:30

您是否在 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.

魂牵梦绕锁你心扉 2024-09-11 01:53:30

是的...已知的“问题”,假设这是 ListView 的行为。

如何解决此问题:

  1. 按照 @HXCaine 的建议观看视频的前 15 分钟,其中解释了 ViewHolder。

  2. 如果我没记错的话,您的示例应该在位图为空时设置默认图像!在示例中,您不将其提供给视图,因此它会被缓存。 Shur 这应该由框架处理,但事实并非如此:(。

示例代码:

public class DebtAdapter extends BaseAdapter {
...

    @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            ViewHolder holder;
            Bitmap bitmap;

            if(convertView == null)  
            {
                convertView = inflater.inflate(viewResourceId, null);

                holder = new ViewHolder();
                holder.photo = (ImageView) convertView.findViewById(R.id.photo);

                convertView.setTag(holder);
            }
            else 
            {
                holder = (ViewHolder) convertView.getTag();
            }
            bitmap = item.getContact().getPhoto();
            if(bitmap != null)
            {
                holder.photo.setImageBitmap(bitmap);
            }
            else
            {
                holder.photo.setImageBitmap(null);
            }
            return convertView;
        }
}

我希望它有帮助。

Yup... known "problem", let say this is the behaviour of the ListView.

How to fix it:

  1. Watch the first 15min of the video as suggested by @HXCaine, which explains the ViewHolder.

  2. 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:

public class DebtAdapter extends BaseAdapter {
...

    @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            ViewHolder holder;
            Bitmap bitmap;

            if(convertView == null)  
            {
                convertView = inflater.inflate(viewResourceId, null);

                holder = new ViewHolder();
                holder.photo = (ImageView) convertView.findViewById(R.id.photo);

                convertView.setTag(holder);
            }
            else 
            {
                holder = (ViewHolder) convertView.getTag();
            }
            bitmap = item.getContact().getPhoto();
            if(bitmap != null)
            {
                holder.photo.setImageBitmap(bitmap);
            }
            else
            {
                holder.photo.setImageBitmap(null);
            }
            return convertView;
        }
}

I hope it helps.

做个少女永远怀春 2024-09-11 01:53:30

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.

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