简体   繁体   中英

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

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

需要考虑的事情-Instagram不会像您想象的那样在设备上缓存每个图像-否则设备的内存可能已用尽,请查看一些图像加载库示例,一个好的开始可能是Square的PicassoUniversal Image Loader ,它们使用导致生成图像的URL(我假设您是通过凌空请求从服务器获取的)来实现您要的相同缓存机制,但它与您当前的会话有关,因此加载非常快速,因此您不会感到有什么不同,我也建议您多读一些,事情并不总是像看起来那样:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM