Downloading images from the Internet is a frequently used feature in project development. Let's summarize it again.
1. Normal Download Method
Layout file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:src="@drawable/icon" android:layout_width="wrap_content" android:id="@+id/imgPic" android:layout_gravity="center|center_vertical" android:layout_height="fill_parent"> </ImageView> </LinearLayout>
Java files
Public class downloadimage extends activity {private imageview imgpic; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. download_image); imgpic = (imageview) findviewbyid (R. id. imgpic); string url = "http://ww1.sinaimg.cn/bmiddle/6834c769jw1djjf4p3p9rj.jpg"; loadrmoteimage (URL);}/*** @ Param imgurl * URL of the Remote Image File ** download remote image */private v Oid loadrmoteimage (string imgurl) {URL fileurl = NULL; Bitmap bitmap = NULL; try {fileurl = new URL (imgurl);} catch (malformedurlexception ERR) {err. printstacktrace ();} Try {httpurlconnection conn = (httpurlconnection) fileurl. openconnection (); Conn. setdoinput (true); Conn. connect (); inputstream is = Conn. getinputstream (); int length = (INT) Conn. getcontentlength (); If (length! =-1) {byte [] imgdata = new byte [length]; byte [] buffer = new byte [512]; int readlen = 0; int destpos = 0; while (readlen = is. read (buffer)> 0) {system. arraycopy (buffer, 0, imgdata, destpos, readlen); destpos ++ = readlen;} bitmap = bitmapfactory. decodebytearray (imgdata, 0, imgdata. length) ;}} catch (ioexception e) {e. printstacktrace ();} imgpic. setimagebitmap (Bitmap );}
2. Download with progress bar
Sometimes the network is poor, or the picture is too large, black screen may occur, the user experience is poor, so adding a progress bar is a good way to improve the user experience
/*** @ Author xushilin XSL xushilin@kingtoneinfo.com * @ version: Creation Time: 02:55:56 * Description: Download images in Android * modification history: */public class downloadimage extends activity {private imageview imgpic; private progressbar; private int totalsize = 0; private int size = 0; private handler mhandler; string url = "http://ww1.sinaimg.cn/bmiddle/6834c769jw1djjf4p3p9rj.jpg "; private Bitmap bitmap = NULL; @ overridepr Otected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. download_image); imgpic = (imageview) findviewbyid (R. id. imgpic); progressbar = (progressbar) findviewbyid (R. id. progressbar); progressbar. setprogress (getprogressint (progressbar. getmax (); mhandler = new handler () {public void handlemessage (Message MSG) {progressbar. setprogress (getprogressint (PR Ogressbar. getmax (); If (Bitmap! = NULL) {imgpic. setimagebitmap (Bitmap) ;}}; new thread () {public void run () {loadrmoteimage (URL );}}. start ();}/*** @ Param imgurl * Remote Image File URL ** download remote image */private void loadrmoteimage (string imgurl) {URL fileurl = NULL; try {fileurl = new URL (imgurl);} catch (malformedurlexception ERR) {err. printstacktrace ();} Try {httpurlconnection conn = (httpurlconnection) fileurl. openconnection (); Conn. setdoinput (true ); Conn. Connect (); inputstream is = conn. getinputstream (); int length = (INT) Conn. getcontentlength (); totalsize = length; If (length! =-1) {byte [] imgdata = new byte [length]; byte [] buffer = new byte [512]; int readlen = 0; int destpos = 0; while (readlen = is. read (buffer)> 0) {system. arraycopy (buffer, 0, imgdata, destpos, readlen); destpos + = readlen; size = destpos; mhandler. sendemptymessage (1); thread. sleep (100);} bitmap = bitmapfactory. decodebytearray (imgdata, 0, imgdata. length); mhandler. sendemptymessage (1) ;}} catch (ioexception e) {e. Printstacktrace ();} catch (interruptedexception e) {e. printstacktrace () ;}} private int getprogressint (INT max) {int result = (totalsize> 0 )? (INT) (size * max * 1.0/totalsize): 0; return result ;}}
The effect is as follows:
Download Process
Download complete: