我如何使用Android LRU缓存,图片来自我的服务器 [英] How do I use android LRU cache with images from my server

查看:241
本文介绍了我如何使用Android LRU缓存,图片来自我的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何使用Android LRU缓存,这样我可以在我的应用程序中使用它。

I am trying to understand how to use android LRU cache so that I can use it in my app.

我有一个应用程序,是关于图像。一个活动显示图像的一个GridView,另一个显示全屏图像,而除了这两个活动图像在各种其他方式使用。现在,我的图片的约85%是由我的服务器来。所以,我对LRU缓存的问题是:我直接从服务器到LRU缓存加载图像?或者,需要加载这些本地存储的图像转换成LRU缓存当我加载从服务器图像到我的应用程序的磁盘空间,然后?

I have an app that is all about images. One activity displays a GridView of images, another displays a fullscreen image, and besides those two activities images are used in all sorts of other ways. Now about 85% of my images are coming from my server. So my question about LRU cache is this: do I load images directly from the server into LRU cache? Or do I load images from the server into my app's disk space and then when needed load those locally stored images into LRU cache?

原因我的困惑是这样的。约15%的我的应用程序的图像使用的来自用户的本地设备,它的应用程序通过一个意图获得

The reason for my confusion is this. About 15% of my app's image usage comes from the user's local device, which the app gets through an intent

public void dispatchGalleryIntent(View view) {
  Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  gallery.setType("image/*");
  startActivityForResult(gallery, GALLERY_PHOTO_REQUEST_CODE);
}

显然,我可以加载从本地图像文件夹到LRU缓存中获得的这些图片。之前,我在LRU缓存中读取的时候,我是这些地方的图像几乎已经被缓存的IM pression下。不管怎样,我的计划是只缓存我从服务器获取图像。但我想知道我是否应该把它们存储在我的应用程序的内部存储,然后将它们传递给LRU缓存需求,还是在LRU缓存已经为我处理存储。换句话说,如果我的应用程序被杀害,并造成LRU缓存清空?要重申我的关注,我想从服务器获得生存崩溃和应用程序的图像被杀害。所以LRU缓存本身适用于或者我应该保存我的磁盘映像,然后根据需要加载LRU缓存?

Apparently I can load these images obtained from the local image folder into LRU cache. Before I read up on LRU cache, I was under the impression that these local images were practically already cached. Anyway, my plan is to only cache the images I get from the server. But I am wondering if I should store them on my app's internal storage and then pass them to LRU cache on demand or does LRU cache already handles storage for me. In other words, if my app is killed, does that cause LRU cache to empty? To restate my concern, I want images obtained from the server to survive crashes and the app being killed. So is LRU cached itself suitable for that or should I store my images on disk and then load LRU cache as needed?

推荐答案

该LRUCache是​​在内存中缓存的对象。它无关的文件系统上的任何东西,它不是跨会话持续性。如果你想从服务器下载文件,并让他们留下来,你需要将它们保存到文件系统中。然后,如果你正在处理大量的图片,你可能想要把位图对象到LRUCache。

The LRUCache is an in memory cache for objects. It has nothing to do with anything on the file system, and it is not persistent across sessions. If you want to download files from a server and have them stick around, you need to save them to the file system. Then if you're dealing with a lot of images you may want to put the Bitmap objects into an LRUCache.

这篇关于我如何使用Android LRU缓存,图片来自我的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆