Вопрос

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

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?

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);
}

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top