Original article address:
Http://bbs.chinaunix.net/thread-1926876-1-1.html
In some cases, we need to download images from the remote server on the Android device for display. Here we have two better methods to download remote images. Method 1: Ask the remote server directly through the HTTP class access provided by the android official website. Here, androidhttpclient is the new method in SDK 2.2, and the API level is 8. Please note that, static access can be called directly. If the SDK version is low, you can consider the Apache HTTP library. Of course, httpurlconnection or urlconnection can also be used. Static bitmap downloadbitmapbycwj (string URL) {final androidhttpclient client = androidhttpclient. newinstance ("Linux March"); Final httpget getrequest = new httpget (URL); try {httpresponse response = client.exe cute (getrequest); Final int statuscode = response. getstatusline (). getstatuscode (); If (statuscode! = Httpstatus. SC _ OK) {log. E ("cwjdebug", "error" + statuscode + "while retrieving bitmap from" + URL); return NULL;} final httpentity entity = response. getentity (); If (entity! = NULL) {inputstream = NULL; try {inputstream = entity. getcontent (); Final Bitmap bitmap = bitmapfactory. decodestream (inputstream); Return bitmap;} finally {If (inputstream! = NULL) {inputstream. close ();} entity. consumecontent () ;}} catch (exception e) {getrequest. abort (); log. E ("debug", "error while retrieving bitmap from" + URL, E. tostring ();} finally {If (client! = NULL) {client. close () ;}return NULL;} copy the code to remind everyone that the decodestream method of the bitmapfactory class cannot obtain complete data when the network times out or is slow, here, we use the Skip method inherited from the filterinputstream class to forcibly implement the data in the flush stream. The main principle is to check whether the data ends at the end of the file and tell the HTTP class whether to continue. Static class flushedinputstream extends filterinputstream {public flushedinputstream (inputstream) {super (inputstream) ;}@ override public long SKIP (long n) throws ioexception {long totalbytesskipped = 0l; while (totalbytesskipped <n) {long bytesskipped = in. skip (n-totalbytesskipped); If (bytesskipped = 0l) {int byte = read (); If (byte <0) {break; // we reached EOF} else {Tesskipped = 1; // we read one byte} totalbytesskipped + = bytesskipped;} return totalbytesskipped ;}} method 2: Use asynctask's asynchronous task to start from Android 1.5 firmware development platform. Google provides an asynctask class to help developers process asynchronous download implementation, compared with the thread, it can run in the UI thread. Its internal implementation is derived from Java 5 and concurrent, the overall implementation is relatively reliable, that is, the resource usage is slightly larger. However, it is easier to use. Here, the download method of the image-like imagedownloader class can be used to perform operations such as UI display. Parameter 1 URL is the URL of the file on the remote server, and the second parameter is the imageview object, you can directly display the downloaded Remote Image in the imageview. Public class imagedownloader {public void download (string URL, imageview) {bitmapdownloadertask task = new bitmapdownloadertask (imageview); task.exe cute (URL );}}} copy the code to implement the specific asynctask class. Considering that the image size may be large, we recommend that you use weak references to save the imageview object in order to provide sufficient space for JVM storage. Class bitmapdownloadertask extends asynctask <string, void, bitmap> {private string URL; private final weakreference <imageview> imageviewreference; // use weakreference to solve memory problems public bitmapdownloadertask (imageview) {imageviewreference = new weakreference <imageview> (imageview) ;}@ override protected bitmap doinbackground (string... params) {// The actual download thread is actually a concurrent thread internally, so it will not block return downloadbi TMAP (Params [0]);} @ override protected void onpostexecute (Bitmap bitmap) {// If (iscancelled () {bitmap = NULL after download ;} if (imageviewreference! = NULL) {imageview = imageviewreference. Get (); If (imageview! = NULL) {imageview. setimagebitmap (Bitmap); // After the download, set imageview to the bitmap object you just downloaded }}}}