image To Byte : Image « 2D Graphics « Android






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();
  }
}

   
    
    
  








Related examples in the same category

1.Capture Image
2.extends BaseAdapter to create Image adapter
3.extends BaseAdapter to create adapter for images
4.Capture Image and display
5.Load up the image's dimensions
6.Lazy Loading Image
7.Resize Image
8.Fit Image No Margin
9.To Rotate Texture Image
10.Image Resize
11.Create Image and resize Image
12.Image Loader
13.Save Image and Text to SD card
14.Scale and rotate Image
15.create Image
16.Resize Photo