可以把bitmap圖片和base64字符串來互相轉換~ 從此媽媽再也不用擔心我處理bitmap啦~
/**
* 將bitmap轉換成base64字符串
*
* @param bitmap
* @return base64 字符串
*/
public String bitmaptoString(Bitmap bitmap, int bitmapQuality) {
// 將Bitmap轉換成字符串
String string = null;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, bitmapQuality, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}
/**
* 將base64轉換成bitmap圖片
*
* @param string base64字符串
* @return bitmap
*/
public Bitmap stringtoBitmap(String string) {
// 將字符串轉換成Bitmap類型
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}