我按以下方式对图像进行编码并将其存储在我的数据库中:
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
现在我尝试通过以下方式对其进行解码并将其显示在 ImageView
中:
try{
InputStream stream = new ByteArrayInputStream(image.getBytes());
Bitmap bitmap = BitmapFactory.decodeStream(stream);
return bitmap;
}
catch (Exception e) {
return null;
}
}
但是,ImageView
仍为空白,并且不显示图像。我错过了什么吗?
最佳答案
首先尝试从 Base64 解码字符串。
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
在你的情况下:
try{
byte[] decodedByte = Base64.decode(input, 0);
InputStream stream = new ByteArrayInputStream(decodedByte);
Bitmap bitmap = BitmapFactory.decodeStream(stream);
return bitmap;
}
catch (Exception e) {
return null;
}
关于java - 位图 - Base64 字符串 - 位图转换 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35677987/