tag: Android HTTP Java OS Io CTI ar new
/*** Get the network image ** Note: * <uses-Permission Android: Name = "android. permission. internet "/> */private bitmap getbitmapfromnetwork (string imageurl) {url = NULL; Bitmap bitmap = NULL; inputstream = NULL; httpurlconnection = NULL; bytearrayoutputstream = NULL; try {url = new URL (imageurl); httpurlconnection = (httpurlconnection) URL. openconnection (); httpurlconnection. setcon Necttimeout (5*1000); httpurlconnection. setreadtimeout (10*1000); httpurlconnection. setdoinput (true); httpurlconnection. setdooutput (true); If (httpurlconnection. getresponsecode () = httpstatus. SC _ OK) {inputstream = httpurlconnection. getinputstream (); bytearrayoutputstream = new bytearrayoutputstream (); int Len = 0; byte [] buffer = new byte [1024]; while (LEN = inputstream. read (buffer ))! =-1) {bytearrayoutputstream. write (buffer, 0, Len); bytearrayoutputstream. flush ();} byte [] imagedata = bytearrayoutputstream. tobytearray (); bitmap = bitmapfactory. decodebytearray (imagedata, 0, imagedata. length);} else {system. out. println ("image request failed") ;}} catch (exception e) {system. out. println ("e =" + E. tostring ();} finally {try {If (bytearrayoutputstream! = NULL) {bytearrayoutputstream. Close () ;}if (inputstream! = NULL) {inputstream. Close ();} If (httpurlconnection! = NULL) {httpurlconnection. disconnect () ;}} catch (exception e) {system. out. println ("e =" + E. tostring () ;}} return bitmap;}/*** get the network image and save it to sdcard * Note: * <uses-Permission Android: Name = "android. permission. internet "/> * <uses-Permission Android: Name =" android. permission. write_external_storage "/> */private void getbitmapfromnetworkandsavetosdcard (string imageurl, string filepath) {URL url = NULL; file imagefi Le = NULL; httpurlconnection = NULL; fileoutputstream = NULL; bytes = NULL; inputstream = NULL; bufferedinputstream = NULL; try {url = new URL (imageurl ); httpurlconnection = (httpurlconnection) URL. openconnection (); httpurlconnection. setconnecttimeout (5*1000); httpurlconnection. setreadtimeout (10*1000); httpurlconnection. s Etdoinput (true); httpurlconnection. setdooutput (true); If (httpurlconnection. getresponsecode () = httpstatus. SC _ OK) {imagefile = new file (filepath); If (! Imagefile. getparentfile (). exists () {imagefile. getparentfile (). mkdirs () ;}if (! Imagefile. exists () {imagefile. createnewfile ();} fileoutputstream = new fileoutputstream (imagefile); bufferedoutputstream = new bufferedoutputstream (fileoutputstream); inputstream = httpurlconnection. getinputstream (); bufferedinputstream = new bufferedinputstream (inputstream); int Len = 0; byte [] buffer = new byte [1024]; while (LEN = bufferedinputstream. read (buffer ))! =-1) {bufferedoutputstream. write (buffer, 0, Len); bufferedoutputstream. flush () ;}} else {system. out. println ("image request failed") ;}} catch (exception e) {system. out. println ("e =" + E. tostring ();} finally {try {If (fileoutputstream! = NULL) {fileoutputstream. Close ();} If (bufferedoutputstream! = NULL) {bufferedoutputstream. Close () ;}if (inputstream! = NULL) {inputstream. Close ();} If (bufferedinputstream! = NULL) {bufferedinputstream. Close () ;}if (httpurlconnection! = NULL) {httpurlconnection. Disconnect () ;}} catch (exception e) {system. Out. println ("e =" + E. tostring ());}}}