Android asynchronous loading data and images save ideas for a detailed _android

Source: Internet
Author: User
Tags wrapper delete cache

Save the image data obtained from the network on the SD card,

Let's just add the permissions.

Network Permissions Android.permission.INTERNET

SD card Read and write access

Android.permission.MOUNT_UNMOUNT_FILESYSTEMS
Android.permission.WRITE_EXTERNAL_STORAGE

Overall layout

Write interface, use ListView, create an entry layout file, ImageView horizontally TextView

Gets the ListView object in the activity, calls the Setadapter () method, and sets an adapter

Create a new package Adapter, create a new adapter Contactsadapter inheritance system Baseadapter

Create a new domain package, create a contact JavaBean, attribute id,name,image, parameter constructor

Create a new service pack, create a new ContactService business class, create a new static method Getcontacts (), get the contact's, getimages () get the picture

Open a new thread, use the contactservice.getcontacts () inside to get network data, return list object, use handler to pass data to main thread

Create an SD card directory that caches pictures

Gets the File object, through New file (), to the cache folder below the SD card root directory, parameters:

Environment.getexternalstoragedirectory (), "Cache" folder name

Invokes the exists () method of the file object to determine whether the directory exists, is created without it, and calls the file object's Mkdirs ()

Show ListView

Sets the constructor of the Contactsadapter class, passing in Parameters: data, layout file, cache directory file object

Overriding the GetCount () method, the number of bars of the return data

Overrides the GetItem () method, returns the data in the collection based on the index, the Get () method of the list object, Parameters: Index

Overrides the Getitemid () method, which generally returns the index of the data

Rewrite the GetView () method, passing in the parameters: Position index, Convertview

Convertview is a cached view object that, when the first screen is NULL, determines that if NULL, the layout shim is invoked to populate the item layout file.

Locate the control object from the View object and place it in the wrapper object

Because the Findviewbyid () method is very performance-intensive, use the inner class datawrapper to wrap the found two control objects

Then call the Settag () method of the cached view object, parameter: wrapper object

If the cached object is not NULL, it invokes the Gettag () method of the cached object, gets the wrapper object, and gets the control object

Invoke the SetText () display text of the TextView object

Show the picture this place, very time-consuming, if the direct loading is easy to ANR, so to load the picture asynchronously

Load and save pictures asynchronously

Open the thread to execute the code that loads the picture

Implement the GetImage () method in the ContactService business class, read the picture through the get way, obtain the URI object, parameter: Picture path,

Gets the local files file object, via new file (), Parameter: Cache directory object, picture file name

The file name of the picture is saved by MD5 (), gets the file suffix, intercepts from the last point, Path.substring (Path.lastindexof ("."))

To determine the existence of a file directly returns the URI object of the file, called Uri.fromfile (), Parameter: File object

Get gets network data, gets input stream, loops read save

The read input stream is written to the file output stream

Returns the URI object

Unable to update UI in child thread, update UI with handler technology

In the Handlemessage method inside the handler inner class, get to the URI object

Invokes the Setimageuri () method of the ImageView object, showing the picture, parameter: Uri object

Clear Cache

Clears all cached files when the activity exits

Rewrite the activity's OnDestroy () method

Loop for (file File:cache:listFiles ()), call the Delete () method of the File object

Delete Cache Directory

At this point, if the number is very large, will open a lot of threads, also very consuming resources

Asynctask Technology uses (Handler + thread + thread pool) to limit the number of threads opened

Today, I probably introduced the Android asynchronous loading data and image preservation ideas, follow-up will give you to organize specific implementation code, more wonderful information please pay attention to cloud Habitat community website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.