1. 程式人生 > >本地讀取圖片轉化為byte[]

本地讀取圖片轉化為byte[]

public void read(){
String imagePath = "d:/lxp.bmp";
byte[] tmp = new byte[4096];

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
InputStream in = null;
try {
in = new FileInputStream(imagePath);
int len;
while((len = in.read(tmp)) != -1){
buffer.write(tmp,0,len);
}
imageData = buffer.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}