“无法绘制回收的位图";在附加到适配器的图库中显示位图时 [英] "Cannot draw recycled bitmaps" when displaying bitmaps in Gallery attached to Adapter

查看:73
本文介绍了“无法绘制回收的位图";在附加到适配器的图库中显示位图时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android 4.1 a中,对我来说,我们的应用中出现了看似奇怪的错误.在应用程序中,将扩展BaseAdapter的自定义适配器附加到Gallery小部件.当从左到右快速滚动时,反之亦然,我得到一个带有异常消息的FC:

In Android 4.1 a, to me, seemingly strange error occurs in our app. In the app a custom adapter extending BaseAdapter is attached to a Gallery widget. When scrolling fast left-to-right and vice versa I get a FC with the exception message:

java.lang.IllegalArgumentException:无法绘制回收的位图

java.lang.IllegalArgumentException: Cannot draw recycled bitmap

getView(..)方法的代码如下:

Code for the getView(..) method is as follows:

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

    if (convertView == null){
        // View is not recycled. Inflate the layout.
        LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.gallery_image, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.image = (ImageView) convertView.findViewById(R.id.gallery_image);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder)convertView.getTag();
        viewHolder.image.setImageDrawable(null);
    }

    imageLoader.displayImage(images.get(position).getFilename(),
            images.get(position).getUrlThumbnail(),
            viewHolder.image,
            Math.round(BitmapUtil.convertDpToPixel(400f, context)),
            Math.round(BitmapUtil.convertDpToPixel(400f, context)));

    return convertView;
}

我想我应该在某个地方使ImageView为空,但是我无法使其正常工作. ImageLoader是一个非常简单的类,用于从LruCache,磁盘/sdcard或远程获取图像来加载图像.

I guess I should null the ImageView somewhere, but I cannot get it to work correctly. ImageLoader is a (quite) simple class for loading the images - either from LruCache, disk/sdcard or fetch it remotely.

推荐答案

事实证明,此错误是由于在我的类中重写LruCache的entryRemoved(..)方法中调用oldBitmap.recycle()引起的.由于位图可能仍会附加到ImageView上,因此调用recycle()会造成麻烦.

It turns out that this error was caused by calling oldBitmap.recycle() in the entryRemoved(..) method in my class overriding LruCache. As the bitmap might still be attached to the ImageView calling recycle() will cause trouble.

如果我理解正确:LruCache的缓存大小将在其构造函数中设置.当项目数超过此大小时,对象将有资格进行垃圾回收,这将在位图不再与ImageView关联时发生.

If I understand correctly: LruCache's cache size will be set in its constructor. When the number of items exceeds this size the objects will be eligible for garbage collection, which will happen when the bitmap is no longer associated with an ImageView.

网络上的许多示例和教程都建议在entryRemoved(..)中调用recycle(),但是从我的角度来看这是错误的.

A lot of examples and tutorials around the web suggest that recycle() should be called in entryRemoved(..), but from what I can see this is wrong.

这篇关于“无法绘制回收的位图";在附加到适配器的图库中显示位图时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆