Downloads for Android Web images

Source: Internet
Author: User

Downloads for Android Web images

1 /**2 * Get image from Newwork3      * 4      * @paramPath5 * The path of image6      * @returnbyte[]7      * @throwsException8      */9      Public byte[] GetImage (String path)throwsException {TenURL url =NewURL (path); OneHttpURLConnection conn =(HttpURLConnection) url.openconnection (); AConn.setconnecttimeout (5 * 1000); -Conn.setrequestmethod ("GET"); -InputStream instream =Conn.getinputstream (); the         if(Conn.getresponsecode () = =HTTPURLCONNECTION.HTTP_OK) { -             returnReadstream (instream); -         } -         return NULL; +     } -  +     /** A * Get data from Stream at      *  -      * @paraminstream -      * @returnbyte[] -      * @throwsException -      */ -      Public Static byte[] Readstream (InputStream instream)throwsException { inBytearrayoutputstream OutStream =NewBytearrayoutputstream (); -         byte[] buffer =New byte[1024]; to         intLen = 0; +          while(len = instream.read (buffer))! =-1) { -Outstream.write (buffer, 0, Len); the         } * outstream.close (); $ instream.close ();Panax Notoginseng         returnOutstream.tobytearray (); -     } the  +     /** A * Save File the      *  +      * @paramBM -      * @paramFileName $      * @throwsIOException $      */ -      Public voidSaveFile (byte[] data, String fileName)throwsIOException { -File Dirfile =NewFile (constant.path_pic); the         if(!dirfile.exists ()) { - Dirfile.mkdir ();Wuyi         } theFile Mycapturefile =NewFile (Constant.path_pic +fileName); -Bufferedoutputstream BOS =NewBufferedoutputstream ( Wu                 NewFileOutputStream (Mycapturefile)); -Bitmap Bitmap = bitmapfactory.decodebytearray (data, 0, data.length); AboutBitmap.compress (Bitmap.CompressFormat.JPEG, 80, BOS); $ Bos.flush (); - bos.close (); -     } -  A     /** + * Load local image the      *  -      * @paramURL $      * @return the      */ the      Public StaticBitmap getloacalbitmap (String url) { the         Try { theFileInputStream FIS =Newfileinputstream (URL); -             returnBitmapfactory.decodestream (FIS);///Turn the flow into bitmap pictures in  the}Catch(FileNotFoundException e) { the e.printstacktrace (); About             return NULL; the         } the}

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.