Get the network image (Bitmap) to the memory or SD card,

Source: Internet
Author: User

Get the network image (Bitmap) to the memory or SD card,

/*** Get the network image ** Note: * <uses-permission android: name = "android. permission. INTERNET "/> */private Bitmap getBitmapFromNetWork (String imageUrl) {URL = null; Bitmap bitmap = null; InputStream inputStream = null; HttpURLConnection httpURLConnection = null; ByteArrayOutputStream 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 httpURLConnection = null; FileOutputStream fileOutputStream = null; bytes = 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. 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 ());}}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.