Android remotely gets pictures and caches them locally

For client-server applications, remote image acquisition is a frequently used function, and image resources often consume large traffic. For applications, if this problem is not handled properly, users will collapse and unknowingly run out of mobile traffic. When users find that your application consumes his mobile traffic, Then you can imagine what fate your application will face. Another problem is the loading speed. If the image loading speed in the application is very slow, the user will also wait until it crashes. So how to deal with the acquisition and management of picture resources?

1. Asynchronous Download: as we all know, in Android applications, if the UI thread does not respond for 5 seconds, it will throw a non response exception. For remote access to large resources, this exception is still easy to throw out. So how to avoid this problem. There are two ways to do this in Android:

Start a new thread to obtain resources, send messages through the handler mechanism after completion, and process messages in the UI thread, so as to obtain pictures in the asynchronous thread, and then update the UI thread through the handler message. Use the asynctask provided in Android to complete.

The specific methods are not introduced here. Just check the API, or Google and Baidu. This is mainly about local cache.

2. Local cache: for image resources, you can't let the application download the listview remotely every time you get it, which will waste resources, but you can't put all image resources into memory (although it will load faster). Because image resources often occupy a lot of memory space, it is easy to cause oom. So what if the downloaded pictures are saved to the sdcard and obtained directly from the sdcard next time? This is also a practice. I have seen that many applications use this method. Some algorithms such as LRU can ensure that the sdcard occupies only a small part of the space, which not only ensures the loading of pictures, saves traffic, but also makes the sdcard occupy only a small part of the space. Another way is to save resources directly in memory, and then set expiration time and LRU rules.

Sdcard saving: to open up a certain space on the sdcard, you need to first judge whether the remaining space on the sdcard is enough. If it is enough, you can open up some space, such as 10m. When you need to obtain a picture, you should first find it from the directory on the sdcard. If you find it, use the picture and update the last time the picture is used. If it cannot be found, download the image from the server through the URL. If the download succeeds, put it on the sdcard and use it. If it fails, there should be a retry mechanism. Like three times. After the download is successful, save it to the sdcard. First judge whether the 10m space has been used up. If not, save it. If the space is insufficient, delete some resources that have not been used up recently according to LRU rules. Key code: save picture to SD card:

Calculate space on sdcard:

Last modification time of modified file:

Local cache optimization:

File usage time sorting:

Memory saving: if it is saved in memory, only a certain amount can be saved, but it cannot be put in all the time. It is necessary to set the expiration time of data, LRU and other algorithms. Here is a way to put commonly used data into one cache (a) and infrequently used data into another cache (b). When you want to obtain data, first obtain it from A. if it does not exist in a, then obtain it from B. The data in B is mainly the data from the LRU in a. the memory recovery here is mainly aimed at the memory of B, so as to keep the data in a can be hit effectively.

Define a cache first

Redefine B cache:

If the cache does not exist, you can only go to the server to download:

These are two methods. Some applications use thread pool and message queue MQ when downloading, which is more efficient for image downloading. Interested students can have a look.

Summary: for relatively large resources such as remote images, you must obtain local cache in asynchronous threads.

The above is the detailed introduction of Android remote image acquisition and local cache method, hoping to be helpful to your learning.

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
分享
二维码
< <上一篇
下一篇>>