如何在android上缓存数据?

时间:2020-12-08 22:11:11

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

好吧,我正在构建一个应用程序,我必须显示图像和文本作为其标题。我已成功完成构建应用程序,我可以从服务器获取数据并显示它。我的问题是我如何缓存数据(包含图像)并显示数据。例如,在Instagram中我们得到了我们关注的所有人的生成源,我们可以看到带有文本的图像,当我们退出应用程序并再次打开应用程序时,我们可以很容易地看到所有带有文字的图像,因为它与互联网无关。我想实现类似的东西。请帮助我。谢谢

2 个解决方案

#1


2  

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

您可以使用LRUCache(最近最少使用,它首先丢弃最近最少使用的项目)来存储图像。来自Android文档:

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.

我们使用Bitmap方法来确定放入缓存的每个元素的大小。在这个例子中,我们使用1/8的可用堆内存。如果需要,应增加缓存大小。

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:

除此之外,请查看关于“缓存位图”的Android文档,其中说:

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.

内存缓存以占用宝贵的应用程序内存为代价提供对位图的快速访问。 LruCache类(也可在支持库中使用,可用于API级别4)特别适合缓存位图,将最近引用的对象保存在强引用的LinkedHashMap中,并在缓存超过其之前驱逐最近最少使用的成员指定大小。

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

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

#2


1  

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 :)

需要考虑的事项 - Instagram不会像您想象的那样缓存设备上的每个图像 - 否则您的设备内存将耗尽,看看一些图像加载库示例,一个好的开始可能是毕加索广场,或通用Image Loader,他们实现了你所询问的相同的缓存机制,使用一个url(我假设你从服务器使用一个截击请求获得)导致图像,但它与你当前的会话有关,加载非常快,所以你不会感觉到差异,我也建议阅读更多,事情并不总是如他们所说:)

#1


2  

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

您可以使用LRUCache(最近最少使用,它首先丢弃最近最少使用的项目)来存储图像。来自Android文档:

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.

我们使用Bitmap方法来确定放入缓存的每个元素的大小。在这个例子中,我们使用1/8的可用堆内存。如果需要,应增加缓存大小。

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:

除此之外,请查看关于“缓存位图”的Android文档,其中说:

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.

内存缓存以占用宝贵的应用程序内存为代价提供对位图的快速访问。 LruCache类(也可在支持库中使用,可用于API级别4)特别适合缓存位图,将最近引用的对象保存在强引用的LinkedHashMap中,并在缓存超过其之前驱逐最近最少使用的成员指定大小。

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

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

#2


1  

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 :)

需要考虑的事项 - Instagram不会像您想象的那样缓存设备上的每个图像 - 否则您的设备内存将耗尽,看看一些图像加载库示例,一个好的开始可能是毕加索广场,或通用Image Loader,他们实现了你所询问的相同的缓存机制,使用一个url(我假设你从服务器使用一个截击请求获得)导致图像,但它与你当前的会话有关,加载非常快,所以你不会感觉到差异,我也建议阅读更多,事情并不总是如他们所说:)