位图 - Base64 字符串 - 位图转换 android

Bitmap - Base64 String - Bitmap conversion android

我正在按以下方式对图像进行编码并将其存储在我的数据库中:

 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;
    }