我以以下方式编码图像并将其存储在数据库中:
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/