Android asynchronously downloads pictures and caches them to the local demo

In Android development, we often need to download XML or JSON type data from the server, including some image resources. This demo simulates this demand, loading xml resources from the network, including images. What we need to do is to parse the data in XML and cache the images in a local cache directory, And a custom adapter is used to fill in the listview. The running effect of the demo is shown in the figure below:

Through this demo, you should learn the following points

1. How to parse an XML

2. What is the idea of caching pictures to a local temporary directory used in the demo?

3. The asynctask class is used because threads must be started to load data asynchronously. However, when starting threads, the number of threads can not be well controlled. When the number of threads is too large, the mobile phone will soon get stuck. Here, the asynctask class is used to solve this problem. This class encapsulates the thread pool technology, This ensures that too many threads will not consume too many resources

4. Usage of handler class in this demo 5. Usage of custom adapter

The following is the activity in the demo.

In activity, pay attention to the following points,

1. Initialize a cache directory. This directory should be created when the application is started to prepare for the formalities of caching pictures. Here, the data is stored on the sdcard

2. To load data from the server, the time-consuming operation is best to start the thread to load data. After loading, update the UI thread asynchronously. This problem can be solved by using the handler mechanism,

3. When finally exiting the application, delete the cache directory and the data in the directory to avoid creating a lot of junk files for the mobile phone

The following is a service class,

In the serivce class, pay attention to the following points

1.HttpURLConnection conn = (HttpURLConnection) url.openConnection(); Get a link to communicate

2. How to use the xmlpullpaser class to parse XML and encapsulate data into objects

3. The method getimageuri (string path, file cache) is specifically implemented

4.Uri.fromFile(file); This method can return a URI directly

The following is a custom adapter class,

In the custom adapter, we should note that the asyncimagetask class inherits the asynctask class. Asynctask is a class often used for asynchronous tasks in Android. It encapsulates the thread pool. After detailed analysis, we will post a blog later.

The following is the XML file we obtained and parsed from the server

In this demo, for the sake of security, the file name of the downloaded image is also MD5 encrypted. The following is the MD5 encrypted code,

The domain class contact.java is omitted above. Through this demo, it can be seen that asynchronous tasks often need to be processed in Android, so we often use the means of manually starting threads, handler mechanism, or asynctask class to ensure the performance of applications.

The above is the Android asynchronous download picture introduced by Xiaobian to you, and cache the picture to the local demo for detailed explanation. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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