Mega Code Archive

 
Categories / Android / 2D Graphics
 

Image To Byte

//package cn.cate.service; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class ImageUtils {      public static byte[] imageToByte(String imagePath){     InputStream is;     ByteArrayOutputStream bs = new ByteArrayOutputStream();     try {       URL url = new URL(imagePath);       HttpURLConnection conn = (HttpURLConnection)url.openConnection();       conn.setRequestMethod("GET");       conn.setReadTimeout(5 * 1000);       if(conn.getResponseCode() == 200){         is = conn.getInputStream();         byte[] buffer = new byte[1024];         int len = 0;         while((len = is.read(buffer)) != -1){           bs.write(buffer, 0, len);         }       }else{         throw new Exception("????");       }              bs.close();       is.close();     } catch (Exception e) {       e.printStackTrace();     }     return bs.toByteArray();   } }