/**
* bitmap转为base64
*
* @param bitmap
* @return
*/
public static String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
int options = 100;
while (baos.toByteArray().length / 1024 > 100) {
baos.reset();
options -= 5;
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
baos.flush();
baos.close();
byte[] bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
java : 压缩图片并转base64
最新推荐文章于 2024-06-08 13:52:48 发布