How to make Volley NetworkImageView worke offline

前端 未结 6 2123
悲&欢浪女
悲&欢浪女 2021-02-10 18:04

I use Volley NetworkImageView to download images from internet and show in my listview. Now I want to make Volley NetworkImageView show sa

6条回答
  •  执笔经年
    2021-02-10 19:04

    If I understand you correctly, you would benefit if the memory cache provided to the ImageLoader class that's used by your NetworkImageView will be persisted between app runs, without losing the fact that it's a memory cache.

    That memory cache keeps the correctly sized bitmap in normal operation - which you would like available even if the network goes down.

    So here's an idea: every time you're app is closed, persist on file the images from the cache. The next time you load your app, when you create the memory cache - check for a persisted version on the disk, and if it's available - populate the memory cache from the disk.

    There are several approaches you can take to decide when to persist an image and when to delete it.

    Here's one approach: create a hybrid memory / disk cache. It would work exactly the same as your memory cache works now with the following differences:

    1. Every time putBitmap() is called, along with your normal operation, save an encoded version of the bitmap to the disk in a background thread / AsyncTask.
    2. Every time a bitmap is removed from the cache (I'm assuming you have some sort of space constraint on the cache), delete that file from the disk on a background thread / AsyncTask.
    3. Create a "loadFromDisk" task, to be performed in the background every time a memory cache is created (once per app run) to populate your memory cache with the available images from the disk.

    You can't avoid decoding the bitmaps, however you can cut the size and having to deal with resizing large bitmaps.

    Does this help you?

提交回复
热议问题