0

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");

この問題の原因は何ですか?

4

1 に答える 1

0

@Ascorbinのおかげで答えにたどり着きました! 車輪を再発明する必要はありません。私はこのライブラリを使用しました:

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

誰かが私と同じ問題を抱えている場合...つまり、キャッシュされた画像がインターウェブからリロードされる..次に、そのライブラリとこのスニペットを使用して画像を設定します。

        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);
于 2014-01-16T16:53:19.110 に答える