Android supports asynchronous image loading in multiple ways

Source: Internet
Author: User

Remember that the Android app was earlier than Version 2.2. If time-consuming operations, such as HTTP and socket, were performed in the UI thread

Will generateAndroid. OS. networkonmainthreadexception

If the network image is asynchronously loaded,To be performed in a non-UI thread. There are usually four methods:

1. Handler + runnable mode:

Define handler in the activity, and then use the handler. Post (runnable) method. This will be executed in the main thread. If it is sdk3.0 or above, the UI thread will be blocked and an exception will be reported.

2. Handler + thread + message mode:

In handler, The handmessage method is rewritten. The network image loading operation is executed in thread. the handler sends messages and updates the UI in handlermessage.

3. Handler + threadpool (thread pool) + message

Define a thread pool, store five threads, and then use the POST method of handler to perform the UI update operation.

4. Handler + threadpool + message + softreference Cache

First load the image from the cache. If not, load the image from the network and save it to the cache. The other steps are the same as 3.

Obtain network imagesCodeAs follows:

View code

Package Com. Allen. utils;

ImportJava. Io. ioexception;
ImportJava. Io. inputstream;
ImportJava. Lang. Ref. softreference;
ImportJava.net. httpurlconnection;
ImportJava.net. malformedurlexception;
ImportJava.net. url;
ImportJava. util. Map;

ImportAndroid. Graphics. Bitmap;
ImportAndroid. Graphics. bitmapfactory;
ImportAndroid. Graphics. drawable. drawable;
ImportAndroid. util. log;

Public Class Utils {
/**
* Retrieving image resources on the network
* @ Param SRC image address
* @ Return Drawable object
*/
Public Static Drawable LoadImage (string SRC ){
URL url = Null ;
Try {
Url = New URL (SRC );
} Catch (Malformedurlexception e ){
Log. E ("-utils --> malformedurlexcetion --", E. tostring ());
}
Inputstream is = Null ;
Try {
Is = URL. openstream ();
} Catch (Ioexception e ){
Log. E ("-utils --> ioexception --", E. tostring ());
}
Drawable = drawable. createfromstream (is, "IMG ");
Return Drawable;
}
/**
* Download from the network
* @ Param URL
* @ Return
*/
Public Static Bitmap getbitmapfromurl (string URL ){
Bitmap bitmap = Null ;
URL u = Null ;
Httpurlconnection conn = Null ;
Inputstream is = Null ;
Try {
U = New URL (URL );
Conn = (httpurlconnection) U. openconnection ();
Is = conn. getinputstream ();
Bitmap = bitmapfactory. decodestream (is );
} Catch (Exception e ){
E. printstacktrace ();
}
Return Bitmap;
}

/**
* Read from the cache
* @ Param URL
* @ Return
* @ Throws Exception
*/
Public Static Bitmap getimgfromcache (string URL, Map <string, softreference <bitmap> imgcache) Throws Exception {
Bitmap bitmap = Null ;
// Read from memory
If (Imgcache. containskey (URL )){
Synchronized (Imgcache ){
Softreference <bitmap> bitmapreference = imgcache. Get (URL );
If ( Null ! = Bitmapreference ){
Bitmap = bitmapreference. Get ();
}
}
} Else { // Downstream from the network
Bitmap = getbitmapfromurl (URL );
// Save images to memory
Imgcache. Put (URL, New Softreference <bitmap> (Bitmap ));
}
Return Bitmap;
}

}

Download all source code:/files/jaylong/loadimage.zip;

Of course there are other methods, such as using anysctask, timer, timertask ......

 

Related Article

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.