androidbitmapandroid-lru-cache

Implementing Image Lru Caching with DiskLruCache


I am trying to implement Disk based image lru cache with jake wharton DiskLruCache library. link to library. I am using a code snippet from here.

Methods i am having trouble with are this

private boolean writeBitmapToFile(Bitmap bitmap, DiskLruCache.Editor editor)
        throws IOException, FileNotFoundException {
    OutputStream out = null;
    try {
        out = new BufferedOutputStream(editor.newOutputStream(0), Utils.IO_BUFFER_SIZE);
        return bitmap.compress(mCompressFormat, mCompressQuality, out);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

where editor.newOutputStream(0) does not exist, and this one

public Bitmap getBitmap(String key) {

    Bitmap bitmap = null;
    DiskLruCache.Snapshot snapshot = null;
    try {

        snapshot = mDiskCache.get(key);
        if (snapshot == null) {
            return null;
        }
        final InputStream in = snapshot.getInputStream(0);
        if (in != null) {
            final BufferedInputStream buffIn =
                    new BufferedInputStream(in, Utils.IO_BUFFER_SIZE);
            bitmap = BitmapFactory.decodeStream(buffIn);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (snapshot != null) {
            snapshot.close();
        }
    }

        Log.d("cache_test_DISK_", bitmap == null ? "" : "image read from disk " + key);

    return bitmap;

}

where snapshot.getInputStream(0) also does not exist.

What am i doing wrong? I have placed the jar with the library and all is well, are these methods deleted from DiskLruCache library? Is there other way to do this now? I can't find any examples or tutorials.

Library version is the latest disklrucache-2.0.2


Solution

  • Make sure you have imported the right class:

    import com.jakewharton.disklrucache.DiskLruCache