How would you cache numerous large images from the net?

If you are developing an image gallery app like Instagram, caching many (large) images from your server becomes critical. In the quest for optimal performance, you want to present images rapidly, so caching them to avoid reloading them from the network is essential.

Since you can’t cache too many large multi-megabyte images in RAM (since Android memory is volatile and is a limited resource), you really have no alternative but to cache images on disk (flash).

Rather than writing your own cache class(es), you could instead leverage and wrap DiskLruCache. It’s an indispensable open source solution that experienced Android developers should be aware of. It can be used to cache any kind of data to a file system directory, bounded by a size that you specify.

  • 使用 Memory Cache 缓存已下载的图片

    Caching bitmaps, keeping recently referenced objects in a strong referenced LinkedHashMap and evicting the least recently used memory before the cache exceeds its designed size.

  • 使用 Disk Cache 缓存已下载的图片

    JakeWharton/DiskLruCache: Java Implementation of a Disk-based LRU cache. A cache that uses a bounded amount of space on a filesystem.