/*
* 將bitmap轉換爲base64字節數組
*/
public byte[] Bitmap2Base64(Bitmap bitmap) {
try {
// 先將bitmap轉換爲普通的字節數組
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
byte[] buffer = out.toByteArray();
// 將普通字節數組轉換爲base64數組
byte[] encode = Base64.decode(buffer, Base64.DEFAULT);
return encode;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 將base64字節數組轉換爲bitmap
*/
public Bitmap Base642Bitmap(byte[] base64) {
// 將base64字節數組轉換爲普通的字節數組
byte[] bytes = Base64.decode(base64, Base64.DEFAULT);
// 用BitmapFactory創建bitmap
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}