add Bitmap To Memory Cache - Android Graphics

Android examples for Graphics:Bitmap Read

Description

add Bitmap To Memory Cache

Demo Code

/**/* ww  w.  j av  a  2 s  .c o  m*/
     * This method is used to create a sample size for a bitmap given the
     * required size and the Options class for the bitmap.
     * 
     * Run this method after first running
     * 
     * <pre>
     * <code>
     * final BitmapFactory.Options foo = new BitmapFactory.Options();
     * foo.inJustDecodeBounds = true; 
     * BitmapFactory.decodeResource(Resources, int, foo);
     * </code>
     * </pre>
     * 
     * Then set the output to <code>foo.inSampleSize</code> and then decode the
     * image.
     * 
     * (If using the same BitmapFactory, remember to change
     * <code>inJustDecodeBounds</code> back to false.)
     * 
     * This method was taken from the Developer tutorial on the android website
     * (licensed under Creative Commons) The original source can be found here:
     * {@link http
     * ://developer.android.com/training/displaying-bitmaps/load-bitmap.html}
     * 
     * @param options
     *            A bitmap options class created with
     * @param reqWidth
     *            The preferred width of the image.
     * @param reqHeight
     *            The preferred height of the image.
     * @return The sample size (to be used set to options.inSampleSize)
     */
//package com.java2s;

import android.graphics.Bitmap;

import android.support.v4.util.LruCache;

public class Main {
    private static LruCache<String, Bitmap> mMemoryCache;

    public static void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        assert (mMemoryCache != null);
        if (getBitmapFromMemCache(key) == null) {
            mMemoryCache.put(key, bitmap);
        }
    }

    public static Bitmap getBitmapFromMemCache(String key) {
        assert (mMemoryCache != null);
        return mMemoryCache.get(key);
    }
}

Related Tutorials