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}