Method for realizing asynchronous loading of network pictures in Android Application
In fact, I'm very lucky. I can do Android development with two senior brothers after a week. Senior brothers are great gods. As Xiaobai, I can only learn more and work harder. Recently, I've been too busy to have a chance to summarize. Today, I just completed the class of asynchronous loading of Android client images. Here's a record (PS: in fact, I refer to the online open source implementation here)
Principle the principle of loading pictures in listview or GridView is basically the same:
Get from the memory cache first. If you get it, return. If you can't get it, get it from the file cache next. If you get it, return and update it to the memory cache. If you can't get it, proceed to the next step. Download pictures from the network and update the memory cache and file cache
The flow chart is as follows:
At the same time, pay attention to the number of threads. Generally, when loading pictures in listview, everyone starts a new thread to load them. However, when sliding quickly, it is easy to cause oom, so the number of threads needs to be controlled. We can control the number of threads through the thread pool. The specific size of the thread pool needs to be determined according to the processor and business conditions
The method of establishing thread pool is as follows:
File cache class
The memory cache class uses the soft reference, map < string, softreference < bitmap > > cache. You can google the soft reference mechanism. In short, it implements the map and can be recycled when memory is tight without causing memory leakage
Picture loading class
Call method