话不多说,直接上代码
bitmapToString
public static String bitmapToString(Bitmap bitmap) {
//将Bitmap转换成字符串
String string = null;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
//参数1为转换的图片格式,参数二为压缩的百分比,这里是10%
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}
stringToBitmap
public Bitmap stringToBitmap(String 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;
}

本文介绍了一种实用的方法,用于在Android应用程序中将Bitmap对象转换为String,以及从String还原Bitmap。通过使用Base64编码,可以轻松地在网络上传输或在本地存储图片,这对于开发移动应用时处理图像数据特别有用。
4万+

被折叠的 条评论
为什么被折叠?



