1. retrieve images from the network
/*** Get the network image ** @ Param path the network image path * @ return * @ throws exception */public static byte [] getimage (string path) throws exception {URL url = new URL (PATH); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setconnecttimeout (5*1000); Conn. setrequestmethod ("get"); inputstream instream = Conn. getinputstream (); If (Conn. getresponsecode () == 200) {return streamtool. readstream (instream);} return NULL ;}
2. Read data in the stream
/*** Read data in the stream * @ Param instream * @ return * @ throws exception */public static byte [] readstream (inputstream instream) throws exception {bytearrayoutputstream outstream = new bytearrayoutputstream (); byte [] buffer = new byte [1024]; int Len = 0; while (LEN = instream. read (buffer ))! =-1) {outstream. Write (buffer, 0, Len);} outstream. Close (); instream. Close (); Return outstream. tobytearray ();}
3. Display in listview
Public class mainactivity extends activity {private edittext pathtext; private imageview; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); pathtext = (edittext) This. findviewbyid (R. id. path); imageview = (imageview) This. findviewbyid (R. id. imageview); button = (button) This. findviewbyid (R. id. button); butto N. setonclicklistener (new view. onclicklistener () {public void onclick (view v) {string Path = pathtext. gettext (). tostring (); try {byte [] DATA = imageservice. getimage (PATH); If (Data! = NULL) {Bitmap bitmap = bitmapfactory. decodebytearray (data, 0, Data. length); // construct a bitmap imageview. setimagebitmap (Bitmap); // complete the image display} else {toast. maketext (mainactivity. this, R. string. imageerror, 1 ). show () ;}} catch (exception e) {toast. maketext (mainactivity. this, R. string. neterror, 1 ). show (); E. printstacktrace ();}}});}}
Success!