Android:位图的 LruCache 问题

标签 android android-bitmap android-lru-cache

我有一个使用 LruCache 类的全局位图缓存。为 ListView 加载缩略图时,首先使用缓存。它工作正常。

但一个问题是:有时无法在 ListView 上显示缓存中的位图实例。似乎来自缓存的位图不再有效。我已经从缓存中检查了位图是否不为空并且没有被回收,但似乎仍然无法显示这样的位图(即使它不为空并且没有被回收)。

缓存类:

public class ImageCache {

    private LruCache<String, Bitmap> mMemoryCache;

    private static ImageCache instance;

    public static ImageCache getInstance() {
        if(instance != null) {
            return instance;
        }

        instance = new ImageCache();
        instance.initializeCache();

        return instance;
   }

   protected void initializeCache() {

        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

        // Use 1/8th of the available memory for this memory cache.
        final int cacheSize = maxMemory / 8;

        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {

            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // The cache size will be measured in kilobytes rather than
                // number of items.
                return bitmap.getByteCount() / 1024;
            }
        };

    }

    public Bitmap getImage(String url) {
        return this.mMemoryCache.get(url);
    }


    public void cacheImage(String url, Bitmap image) {
        this.mMemoryCache.put(url, image);
    }
}

使用缓存的代码在 Adapter 类中,它是 CursorAdapter 的子类:

        final ImageCache cache = ImageCache.getInstance();

        // First get from memory cache
        final Bitmap bitmap = cache.getImage(thumbnailUrl);
        if (bitmap != null && !bitmap.isRecycled()) {
            Log.d(TAG, "The bitmap is valid");
            viewHolder.imageView.setImageBitmap(bitmap);
        } 
        else {
            Log.d(TAG, "The bitmap is invalid, reload it.");
            viewHolder.imageView.setImageResource(R.drawable.thumbnail_small);

            // use the AsyncTask to download the image and set in cache
            new DownloadImageTask(context, viewHolder.imageView, thumbnailUrl, dir, filepath).execute();
        }   

DownloadImageTask的代码:

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

    private ImageView mImageView;
    private String url;
    private String dir;
    private String filename;
    private Context context;

    public DownloadImageTask(Context context, ImageView imageView, String url, String dir, String filename) {
        this.mImageView = imageView;
        this.url = url;
        this.filename = filename;
        this.dir = dir;
        this.context = context;
        this.cache = cache;
    }

    protected Bitmap doInBackground(String... urls) {
        // String urldisplay = urls[0];
        final Bitmap bitmap = FileUtils.readImage(context, dir, filename, url);

        return bitmap;
    }

    protected void onPostExecute(Bitmap result) {
        final ImageCache cache = ImageCache.getInstance();
        if(result != null) {
            cache.put(url, result);
            mImageView.setImageBitmap(result);
        }

    }
}

任何帮助将不胜感激。谢谢!

更新:我已经关注了 link greywolf82 建议:“处理配置更改”部分。我将以下属性放在我的 Activity 类和两个 fragment 类中:

public LruCache mMemoryCache;

在 Activity 类中,我尝试在调用 fragment 时初始化缓存:

        // Get the cache
        mMemoryCache = mIndexFragment.mRetainedCache;
        if (mMemoryCache == null) {
            final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

            // Use 1/8th of the available memory for this memory cache.
            final int cacheSize = maxMemory / 8;

            // Initialize the cache
            mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {

                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    // The cache size will be measured in kilobytes rather than
                    // number of items.
                    return bitmap.getByteCount() / 1024;
                }
            };

            Log.d(TAG, "Initialized the memory cache");
            mIndexFragment.mRetainedCache = mMemoryCache;
        }

在 fragment 类中: setRetainInstance(true);

然后我将缓存实例传递给适配器构造函数,以便适配器可以使用缓存。

但我仍然遇到同样的问题。

更新 2:

更改为接受 LruCache 实例的两个适配器类:

新闻游标适配器:

public class NewsCursorAdapter extends CursorAdapter {

    private static final String TAG = "NewsCursorAdapter";

    private LruCache<String, Bitmap> cache;

    private Context mContext;

    public NewsCursorAdapter(Context context, LruCache<String, Bitmap> cache) {
        super(context, null, false);
        this.mContext = context;
        this.cache = cache;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        final Setting setting = ApplicationContext.getSetting();
        // Get the view holder
        ViewHolder viewHolder = (ViewHolder) view.getTag();

        final String thumbnail = cursor.getString(NewsContract.Entry.THUMBNAIL_CURSOR_INDEX);
        if(thumbnail != null) {
            String pictureDate = cursor.getString(NewsContract.Entry.PIC_DATE_CURSOR_INDEX);
            final String dir = "thumbnails/" + pictureDate + "/";
            final String filepath = thumbnail + "-small.jpg";
            final String thumbnailUrl = setting.getCdnUrl() + dir + filepath;

            //final ImageCache cache = ImageCache.getInstance();

            // First get from memory cache
            final Bitmap bitmap = cache.get(thumbnailUrl);
            if (bitmap != null && !bitmap.isRecycled()) {
                Log.d(TAG, "The bitmap is valid: " + bitmap.getWidth());
                viewHolder.imageView.setImageBitmap(bitmap);
            } 
            else {
                Log.d(TAG, "The bitmap is invalid, reload it.");
                viewHolder.imageView.setImageResource(R.drawable.thumbnail_small);

                new DownloadImageTask(viewHolder.imageView, thumbnailUrl, dir, filepath).execute();
            }   
        }
        else {
            viewHolder.imageView.setVisibility(View.GONE);
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = inflater.inflate(R.layout.listview_item_row, parent,
                false);
        // Initialize the view holder
        ViewHolder viewHolder = new ViewHolder();

        viewHolder.titleView = (TextView) view.findViewById(R.id.title);
        viewHolder.timeView = (TextView) view.findViewById(R.id.news_time);
        viewHolder.propsView = (TextView) view.findViewById(R.id.properties);
        viewHolder.imageView = (ImageView) view.findViewById(R.id.icon);
        view.setTag(viewHolder);

        return view;
    }

    static class ViewHolder {
          TextView titleView;
          TextView timeView;
          TextView propsView;
          ImageView imageView;

    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        private ImageView mImageView;
        private String url;
        private String dir;
        private String filename;

        public DownloadImageTask(ImageView imageView, String url, String dir, String filename) {
            this.mImageView = imageView;
            this.url = url;
            this.filename = filename;
            this.dir = dir;
        }

        protected Bitmap doInBackground(String... urls) {

            final Bitmap bitmap = FileUtils.readImage(mContext, dir, filename, url);
            return bitmap;
        }

        protected void onPostExecute(Bitmap result) {
            //final ImageCache cache = ImageCache.getInstance();
            if(result != null) {
                cache.put(url, result);
                mImageView.setImageBitmap(result);
            }

        }
    }
}

列表适配器,NewsTopicItemAdapter:

public class NewsTopicItemAdapter extends ArrayAdapter<NewsTopicItem> {

    private Context context = null;

    private EntryViewHolder viewHolder;

    private HeaderViewHolder headerViewHolder;

    private LruCache<String, Bitmap> mCache;

    public NewsTopicItemAdapter(Context context, List<NewsTopicItem> arrayList, LruCache<String, Bitmap> cache) {
        super(context, 0, arrayList);
        this.context = context;
        this.mCache = cache;
    }

    public void setItems(List<NewsTopicItem> items) {
        this.addAll(items);
    }

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

        final NewsTopicItem item = getItem(position);
        View view;
        if(!item.isHeader()) {
            view = this.getEntryView((NewsTopicEntry)item, convertView, parent);
        }
        else {
            view = this.getHeaderView((NewsTopicHeader)item, convertView, parent);
        }

        return view;
    }

    protected View getEntryView(NewsTopicEntry newsItem, View convertView, ViewGroup parent) {

        View view;

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            viewHolder = new EntryViewHolder();

            view = inflater.inflate(R.layout.listview_item_row, parent,
                        false);
            // Initialize the view holder
            viewHolder.titleView = (TextView) view.findViewById(R.id.title);
            viewHolder.timeView = (TextView) view.findViewById(R.id.news_time);
            viewHolder.propsView = (TextView) view.findViewById(R.id.properties);
            viewHolder.imageView = (ImageView) view.findViewById(R.id.icon);
            view.setTag(viewHolder);

        viewHolder.propsView.setText(newsItem.getSource());

        if (newsItem.getThumbnail() != null) {

            final String dir = "thumbnails/" + newsItem.getPictureDate() + "/";
            final String filepath = newsItem.getThumbnail() + "-small.jpg";
            final String thumbnailUrl = "http://www.oneplusnews.com/static/" + dir + filepath;

            //final ImageCache cache = ImageCache.getInstance();

            // First get from memory cache
            final Bitmap bitmap = mCache.get(thumbnailUrl);
            if (bitmap != null && !bitmap.isRecycled()) {
                viewHolder.imageView.setImageBitmap(bitmap);
            } else {
                viewHolder.imageView.setImageResource(R.drawable.thumbnail_small);

                new DownloadImageTask(viewHolder.imageView, thumbnailUrl, dir, filepath).execute();
            }           
        }
        else {
            viewHolder.imageView.setVisibility(View.GONE);
        }

        viewHolder.titleView.setText(newsItem.getTitle());
        viewHolder.timeView.setText(DateUtils.getDisplayDate(newsItem.getCreated()));

        return view;

    }

    protected View getHeaderView(NewsTopicHeader header, View convertView, ViewGroup parent) {

        View view;


            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            headerViewHolder = new HeaderViewHolder();

            view = inflater.inflate(R.layout.news_list_header, parent,
                        false);
            // Initialize the view holder
            headerViewHolder.topicView = (TextView) view.findViewById(R.id.topic);

            view.setTag(headerViewHolder);
            final View imageView = view.findViewById(R.id.more_icon);
            imageView.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    // Start the Fragement
                }
            });

        Topic topic = header.getTopic();
        if(topic.isKeyword()) {
            headerViewHolder.topicView.setText(topic.getName());
        }
        else {
            // This is a hack to avoid error with - in android
            headerViewHolder.topicView.setText(ResourceUtils.getStringByName(context, topic.getName()));
        }

        return view;

    }


    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        private ImageView mImageView;
        private String url;
        private String dir;
        private String filename;

        public DownloadImageTask(ImageView imageView, String url, String dir, String filename) {
            this.mImageView = imageView;
            this.url = url;
            this.filename = filename;
            this.dir = dir;
        }

        protected Bitmap doInBackground(String... urls) {

            final Bitmap mIcon11 = FileUtils.readImage(context, dir, filename, url);
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            //final ImageCache cache = ImageCache.getInstance();
            if(result != null) {
                mCache.put(url, result);
                mImageView.setImageBitmap(result);
            }

        }
    }



    static class EntryViewHolder {
          TextView titleView;
          TextView timeView;
          TextView propsView;
          ImageView imageView;
          TextView topicView;
    }

    static class HeaderViewHolder {
          TextView topicView;
    }
}

更新 3:我附上了来自 eclipse 的调试信息:第一张图片是工作位图,第二张是缓存中的非工作位图。我没有发现任何可疑的东西。

缓存中工作位图的调试信息:

The debug information of the working bitmap from the cache

缓存中非工作位图的调试信息:

The debug information of the non-working bitmap from the cache

最佳答案

终于找到问题所在了。这是因为适配器。如果不需要缩略图,我在适配器中将一些 ImageView 设置为不可见。当用户滚动 ListView 时,此类 ImageView 实例将被重用,但可见性不会更新。

所以缓存本身现在没问题了。解决方案是检查 ImageView 的可见性并在需要时更新它。

无论如何,非常感谢 greywolf82 花时间和关于单例模式的技巧。

关于Android:位图的 LruCache 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27692403/

相关文章:

android - 在 Android 中集成 paypal 时出错?

android - 使用任务修改控件时出现布局渲染错误

java - 添加到 Canvas 时显示蓝色的位图 (android studio)

Android Kotlin - 使用 Bitmap 和 Glide 实现自定义 map 标记

android - 使用 Android 的 lrucache 示例

java - Android LRU 缓存未显示正确的位图?

android - android中textview的百分比如何着色?

android - AppBar折叠/展开时如何保持工具栏固定在顶部?

Android PNG 到位图 --- SkImageDecoder::Factory 返回 null

java - 获取Android LRUCache当前使用的内存