TODO1:將字符串轉(zhuǎn)換成Bitmap類型(Base64字符串轉(zhuǎn)換成圖片)
public Bitmap stringtoBitmap(String imgBase64){
Bitmap bitmap=null;
try {
byte[]bitmapArray;
bitmapArray=Base64.decode(imgBase64, Base64.DEFAULT);
bitmap=BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
TODO2:二進制流轉(zhuǎn)換為Bitmap圖片
public Bitmap getBitmapFromByte(byte[] temp) {
if (temp != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
return bitmap;
} else {
return null;
}
}
TODO3:Bitmap轉(zhuǎn)換為二進制流
private static byte[] bitmapToByte(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imgBytes = baos.toByteArray();
return imgBytes;
}
TODO4:String路徑圖片轉(zhuǎn)二進制
/**
* 照片轉(zhuǎn)byte二進制
*
* @param imagepath 需要轉(zhuǎn)byte的照片路徑
* @return 已經(jīng)轉(zhuǎn)成的byte
* @throws Exception
*/
public static byte[] readStream(String imagepath) throws Exception {
FileInputStream fs = new FileInputStream(imagepath);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while (-1 != (len = fs.read(buffer))) {
outStream.write(buffer, 0, len);
}
outStream.close();
fs.close();
return outStream.toByteArray();
}