Android image three-level cache development

At present, the project cannot use a third party, so only a three-level cache can be built. The three-level cache is divided into memory cache, local cache and network cache; The cache steps are network, memory and local in order, and then memory, local and network in order. When loading pictures, try to use weak references to avoid OOMs caused by too many pictures.

1. In memory cache, Android provides us with lrucache = which maintains a LinkedHashMap. Lrucache can be used to store various types of data. We set its size, which is generally 1 / 8 of the maximum storage space of the system

2. The local cache obtains the local file according to the URL, and MD5 encrypts the URL as the file name to ensure the correctness of the file

MD5 encryption tool class

Local cache

3. The network cache uses asynchronous loading of asynctask for two reasons:

1. Doinbackground runs on the sub thread to do time-consuming network request operations to avoid blocking the main thread;

2. Onpreexecute and onpostexecute are convenient to update UI and improve user experience.

4. Encapsulate the three-level cache to form imageutil. Because the speed of fetching from the memory cache is fast, it is first fetched from the memory cache, not fetched - > fetched from the local cache, not fetched - > fetched from the network cache.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>