Obtain the network image (Bitmap) to the memory or SD card
/*** Obtain the network image ** Note :*
*/Private Bitmap getBitmapFromNetWork (String imageUrl) {URL url = null; Bitmap bitmap = null; InputStream inputStream = null; HttpURLConnection httpURLConnection = null; ByteArrayOutputStream byteArrayOutputStream = null; try {url = new URL (imageUrl); httpURLConnection = (HttpURLConnection) url. openConnection (); httpURLConnection. setConnectTimeout (5*1000); httpURLConnection. setReadTimeout (10*1000); httpURLConnection. setDo Input (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 permission :*
*
*/Private void Merge (String imageUrl, String filePath) {URL url = null; File imageFile = null; HttpURLConnection httpURLConnection = null; FileOutputStream fileOutputStream = null; BufferedOutputStream bufferedOutputStream = null; inputStream inputStream = null; BufferedInputStream bufferedInputStream = null; try {url = new URL (imageUrl); httpURLConnection = (HttpURLConnection) url. openConnection (); HttpURLConnection. setConnectTimeout (5*1000); httpURLConnection. setReadTimeout (10*1000); httpURLConnection. setDoInput (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 ());}}}