【问题标题】:Using cache to store Bitmaps使用缓存存储位图
【发布时间】:2014-02-05 03:46:38
【问题描述】:

我正在使用 LruCache 来存储位图。

这是我为存储位图而运行的异步任务。

new AsyncTask<Void, Void, Bitmap>() {

        @Override
        protected Bitmap doInBackground(Void... args) {
            String url = d.getImageUrl();
            if (url != null && !url.equals("")) {
                try {
                    LogUtils.log("getting image from cache");
                    Bitmap image = imageCache.getBitmapFromMemCache(url);
                    if (image == null) {

                        image = BitmapFactory.decodeStream(new URL(url)
                                .openConnection().getInputStream());
                        LogUtils.log("storing image to cache");
                        imageCache.addBitmapToMemoryCache(url, image);
                    } else {
                        LogUtils.log("got image from cache");
                    }
                    return image;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;

        }

        @Override
        protected void onPostExecute(Bitmap image) {
            if (holder.image != null) {
                if (image != null) {
                    holder.image.setImageBitmap(image);
                    holder.image.setVisibility(View.VISIBLE);
                }   
            }

        }

    }.execute();

这是我的缓存实现:

public class ImageCache {

private static ImageCache instance;

public static ImageCache getInstance(Context context) {
    if (instance == null)
        instance = new ImageCache(context);
    return instance;
}

private LruCache<String, Bitmap> mMemoryCache;

private ImageCache(Context context) {
    // Get memory class of this device, exceeding this amount will throw an
    // OutOfMemory exception.
    final int memClass = ((ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();

    final int cacheSize = (1024 * 1024 * memClass)/8;


    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in bytes rather than number
            // of items.
            return bitmap.getRowBytes() * bitmap.getHeight();
        }
    };
}

public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    LogUtils.log("key: " + key);
    if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    } 
}

public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}

}

在我的清单中我也有:

android:largeheap="true"

问题是在我的 ListView 中......当我向下滚动时......图像从 url 重新加载并且代码到达这一行:

LogUtils.log("storing image to cache");

什么可能导致这个问题?

【问题讨论】:

  • 我建议你使用这个库,它可以下载、显示和缓存图像。无需重新发明轮子。 github.com/nostra13/Android-Universal-Image-Loader
  • 我听从了您的建议,使用时遇到了完全相同的问题: String url = d.getImageUrl(); ImageLoaderConfiguration 配置 = 新 ImageLoaderConfiguration.Builder(v.getContext())。建造(); ImageLoader imageLoader = ImageLoader.getInstance(); imageLoader.init(config); imageLoader.displayImage("", holder.image); imageLoader.displayImage(url, holder.image);

标签: android caching bitmap android-lru-cache


【解决方案1】:

感谢@Ascorbin,我得到了答案!无需重新发明轮子。 我使用了这个库:

https://github.com/nostra13/Android-Universal-Image-Loader

如果有人和我有同样的问题......那就是......从互联网重新加载缓存的图像......然后使用那个库和这个 sn-p 来设置你的图像:

        DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageOnLoading(R.drawable.noimage) // resource or drawable
        .showImageForEmptyUri(R.drawable.noimage) // resource or drawable
        .resetViewBeforeLoading(true)  
        .delayBeforeLoading(0)
        .cacheInMemory(true) 
        .cacheOnDisc(true) 
        .bitmapConfig(Bitmap.Config.ARGB_8888) 
        .build();


        String url = d.getImageUrl();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(v.getContext())
                .defaultDisplayImageOptions(options)
                .build();
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);
        imageLoader.displayImage("", holder.image);
        imageLoader.displayImage(url, holder.image);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-20
    • 2015-12-07
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2016-05-19
    • 2021-02-21
    • 1970-01-01
    相关资源
    最近更新 更多