Breaking News
Home / Android / Android Snippets / How to build simple Hashmap cache in Android?

How to build simple Hashmap cache in Android?

The following snippet shows how to build a simple Bitmap Cache in Android:

import java.util.HashMap;
import android.graphics.Bitmap;

// TODO: Auto-generated Javadoc
/**
 * The Class ImageMemoryCache.
 */
public class ImageMemoryCache {

    /** The cache. */
    private static HashMap<String, Bitmap> cache = new HashMap<String, Bitmap>();

    /**
     * Gets the image.
     *
     * @param key
     *            the key
     * @return the image
     */
    public static Bitmap getImage(String key) {
        if (cache.containsKey(key)) {
            return cache.get(key);
        }
        return null;
    }

    /**
     * Sets the image.
     *
     * @param key
     *            the key
     * @param image
     *            the image
     */
    public static void setImage(String key, Bitmap image) {
        cache.put(key, image);
    }
}

<!>

[pb_builder]

Check Also

How to check if an Android device can connect to Internet?

The following code snippet shows how to check if an Android device can connect to …

Leave a Reply