Sarthak Mishra
Sarthak Mishra

Reputation: 1114

How to cache data on android?

Well i am building an app where i have to display images and text as its caption.I have successfully completed building the app where in i can fetch data from a server and display it. My question is how can i cache the data(containing images)and display the data .For eg in Instagram we get a feed generated of all people we follow and we can see images with text and when we exit the app and open the app again ,we can easily see all the images with text as it is irrespective of the internet.I Want to implement something similar.Please help me. Thank You

Upvotes: 2

Views: 4068

Answers (2)

TommySM
TommySM

Reputation: 3883

Something to consider - Instagram does not cache every image on the device as you may think - or else your devices' memory would have been depleted, have a look at some image loading library examples, a good start could be Picasso by Square, or Universal Image Loader, they implement the same caching mechanism you're asking about, using a url (that I assume you get from your server using a volley request) that leads to the image, but it relates to your current session, the loading is very fast so you won't feel the difference, also I would suggest reading about it some more, things are not always as they seem :)

Upvotes: 1

Smittey
Smittey

Reputation: 2490

You can use an LRUCache (Least Recently Used, which discards the least recently used items first) to store the images. From the Android docs:

A cache that holds strong references to a limited number of values. Each time a value is accessed, it is moved to the head of a queue. When a value is added to a full cache, the value at the end of that queue is evicted and may become eligible for garbage collection.

It's an in-memory cache so there is a finite number of images we can cache. A good approach to determine the cache size to calculate it based on the available heap memory. The following code is an example of this

int memClass = ((ActivityManager)activity.getSystemService( Context.ACTIVITY_SERVICE)).getMemoryClass();
int cacheSize = 1024 * 1024 * memClass / 8;
LruCache cache = new LruCache<String, Bitmap>(cacheSize);

We use a Bitmap method to determine the size of each element put into the cache. In this example we use 1/8 of the available heap memory. The cache size should be increased if required.

public class AppCache extends LruCache<String, Bitmap> 
{
    public AppCache(int maxSize) 
    {
        super(maxSize);
    }

    @Override
    protected int sizeOf(String key, Bitmap value) 
    {
        return value.getByteCount();
    }

    @Override
    protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) 
    {
        oldValue.recycle();
    }

}

In addition to this, check out the Android Documentation on "Caching Bitmaps" which says:

A memory cache offers fast access to bitmaps at the cost of taking up valuable application memory. The LruCache class (also available in the Support Library for use back to API Level 4) is particularly well suited to the task of caching bitmaps, keeping recently referenced objects in a strong referenced LinkedHashMap and evicting the least recently used member before the cache exceeds its designated size.

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

Upvotes: 2

Related Questions