Android Asynchronous Loading Data and Picture Saving Thoughts Detailed Explanation

  • 2021-07-06 11:42:55
  • OfStack

The picture data obtained from the network is stored on the SD card,

Add all the permissions first

Network permissions android. permission. INTERNET

SD Card Read and Write Permission

android.permission.MOUNT_UNMOUNT_FILESYSTEMS
android.permission.WRITE_EXTERNAL_STORAGE

Overall layout

Write interface, use ListView, create layout file of items, ImageView TextView placed horizontally

Get the ListView object in activity, call the setAdapter () method, and set up an adapter

Create a new package Adapter and a new adapter ContactsAdapter inherits the BaseAdapter of the system

Create a new domain package, create a new javaBean of Contact, attributes id, name, image, and parametric constructor

Create a new service package, create a new ContactService business class, create a new static method getContacts (), get contacts, and getImages () get pictures

Open a new thread, use ContactService. getContacts () to achieve network data, return List object, use Handler to transfer data to the main thread

Create an SD card directory for caching pictures

Get File object, through new File (), get cache folder under SD card root directory, parameter:

Environment. getExternalStorageDirectory (), "cache" folder name

Call the exists () method of the File object, judge whether the directory exists or not, create it if it does not exist, and call the mkdirs () of the File object

Show ListView

Set ContactsAdapter class constructor, pass in parameters: data, layout file, cache directory FIle object

Override getCount () method, number of return data

Override the getItem () method to return the data in the collection based on the index, the get () method of the List object, parameter: index

Override the getItemId () method to return the index of the data

Override the getView () method, passing in parameters: position index, convertView

convertView is a cached View object. When the first screen is displayed, the View object is null. If it 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 wrapper object

Because the findViewById () method is very performance-intensive, the inner class DataWrapper is used to wrap the two control objects found under 1

Then call the setTag () method of the cached View object, parameter: wrapper object

If the cache object is not null, the getTag () method of the cache object is called to get the wrapper object and the control object

Call the setText () presentation text of the TextView object

Showing pictures in this place is very time consuming. If it is easy to load directly, anr, so it is necessary to load pictures asynchronously

Load and save pictures asynchronously

Open the thread to execute the code for loading pictures

Realize getImage () method in ContactService service class, read pictures through get mode, get Uri object, parameter: picture path,

Obtain the local file File object, through new FIle (), parameters: cache directory object, picture file name

The file name of the picture is saved through md5 (), get the file suffix, and intercept from the last point, path. substring (path. lastIndexOf ("."))

If you judge the existence of the file, you will directly return the Uri object of the file and call Uri. fromFile (). Parameter: File object

get obtains network data, obtains input stream, and reads and saves it cyclically

Read the input stream and write it to the file output stream

Returns an Uri object

Unable to update UI in sub-thread, update UI with Handler technology

In the handleMessage method in the Handler inner class, the Uri object is obtained

Call the setImageUri () method of the ImageView object and show the picture. Parameter: Uri object

Clear the cache

When activity exits, clear all cached files

Override onDestroy () method of activity

In the loop for (File file: cache: listFiles ()), the delete () method of the File object is called

Delete the cache directory

At this time, if the number is particularly large, many threads will be opened, which is also very resource-consuming

AsyncTask technology uses (Handler + Thread + thread pool) to limit the number of threads opened

Today about Android asynchronous loading data and picture preservation ideas, the follow-up will give you to sort out the specific implementation code, more wonderful information please pay attention to this site!


Related articles: