chrisbanes / Android-BitmapCache

Android-BitmapCache is a specialised cache, for use with Android Bitmap objects.
834 stars 224 forks source link

Lack of ability to use cached image file many time in different resolution #12

Closed MikolajKakol closed 11 years ago

MikolajKakol commented 11 years ago

I was writing an app that has quite a lot of images that sometimes they are shown as thumbnails and sometimes as big picture. My server gives me only big pictures. So I thought that I will use that lib as ultimate cache image for my app so I wouldn't have to worry ever about deleting not used image files, but I stuck with a problem.

How to add some file to cache and then take bitmap with different resolution. Am I missing some easy solution of doing that or it's not designed that way?

chrisbanes commented 11 years ago

Hi, this cache hasn't been designed for your scenario. I could add it to the lib but it's going against my KISS principles.

Anyway you could quite easily do this yourself:

void insertImageWithThumbIntoCache(InputStream is, String url) {
    // Put full size into cache
    CacheableBitmapDrawable fullSizeResult = mBitmapCache.put(url, is);

    // Now create a scaled version of the image
    Bitmap thumbnailBitmap = Bitmap.createScaledBitmap(fullSizeResult.getBitmap(), 
            60, 60, true);

    // Put thumbnail into cache with different url (key)
    mBitmapCache.put(createThumbnailKey(url), thumbnailBitmap);
}

static String createThumbnailKey(String url) {
    return "thumb_" + url;
}
MikolajKakol commented 11 years ago

That would assign a lot of memory for large bitmaps. I think I would need to use LruDiskCache for file caching and BitmapCache just for in memory bitmap caching. Thank you for you help.