[Android] asynchronous data loading and image storage,
Save the image data obtained from the network on the SD card,
Add all permissions first
Network permission android. permission. INTERNET
SD card read and write permissions
Android. permission. MOUNT_UNMOUNT_FILESYSTEMS
Android. permission. WRITE_EXTERNAL_STORAGE
Overall Layout
Write interface, use ListView to create a layout file and horizontally display the ImageView TextView
Obtain the ListView object in the activity, call the setAdapter () method, and set an adapter
Create a new package Adapter and create a new Adapter ContactsAdapter to inherit the BaseAdapter OF THE SYSTEM
Create a domain package and create a Contact's javaBean, property id, name, image, and constructors with parameters.
Create a service package, create a ContactService service class, create a static method getContacts (), get the contact, getImages () Get the image
Start a new thread, use ContactService. getContacts () to obtain network data, return the List object, and use Handler to pass data to the main thread.
Create an SD card directory for cached images
Get the File object. Use new File () to get the cache folder under the root directory of the SD card. parameters:
Environment. getExternalStorageDirectory (), "cache" folder name
Call the exists () method of the File object to determine whether the directory exists. If the directory does not exist, create it. Call the mkdirs () method of the File object ()
Display ListView
Set the constructor of the ContactsAdapter class and pass in the parameters: data, layout FIle, and cache directory FIle object.
Override the getCount () method and return the number of data records
Override the getItem () method and return the data in the set obtained by the index. The get () method of the List object. Parameter: Index
Override the getItemId () method, which generally returns the index of the data.
Override the getView () method and pass the following parameters: position Index and convertView.
ConvertView is a cached View object. When the first screen is displayed, the View object is null. If the value is null, the layout filler is called to fill the entry layout file.
Find the control object through the View object and put it in the packaging object
The findViewById () method consumes a lot of performance. Therefore, use the internal class DataWrapper to wrap the two control objects found.
Then call the setTag () method of the cached View object. Parameter: Wrap object
If the cached object is not null, call the getTag () method of the cached object to obtain the wrapped object and the control object.
Call setText () of the TextView object to display the text
It takes a lot of time to display the image. If anr is easy to load directly, you need to load the image asynchronously.
Asynchronously load and save images
Enable the thread to execute the image loading code
Implement the getImage () method in the ContactService service class, read the image through the get method, and obtain the Uri object. Parameter: Image path,
Get the File object of the local FIle, and use new File (). parameters: cache directory object and Image FIle Name
The image file name is saved by md5 (). Obtain the file suffix and extract it from the last vertex. path. substring (path. lastIndexOf (".").
If a File exists, the Uri object of the File is directly returned, and Uri. fromFile () is called. Parameter: File object
Get gets network data, get input stream, read and save cyclically
Reads the input stream and writes it to the file output stream.
Returns a Uri object.
The UI cannot be updated in the Child thread. The UI is updated using Handler technology.
In the handleMessage method in the Handler internal class, the Uri object is obtained.
Call the setImageUri () method of the ImageView object to display the image. Parameter: Uri object
Clear Cache
When the activity exits, All cached files are cleared.
Override the onDestroy () method of the activity
In the for (File file: cache: listFiles () loop, call the delete () method of the File object.
Delete cache directory
In this case, many threads will be enabled, which also consumes resources.
AsyncTask Technology (Handler + Thread pool) is used to limit the number of threads Enabled
The code is being organized...