【问题标题】:Android convert base64 encoded string into image viewAndroid将base64编码的字符串转换为图像视图
【发布时间】:2013-03-18 23:04:31
【问题描述】:

我想将 base64 编码的字符串转换为位图,这样我就可以把它放在图像视图中,但是会出现类似的错误

D/skia(7490): --- 解码器->解码返回假,位图返回空值

我的代码是:

byte[] imageAsBytes = Base64.decode(imageData);

image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));

【问题讨论】:

  • 你从哪里得到你的base64字符串?你确定它有效吗?
  • 我正在从 web 服务获取 base64 字符串。我们如何检查它是否有效?
  • 能贴出获取Base64字符串的代码吗?
  • ImageData 只是一个 base64 编码的字符串。字符串长度的大小仅与 web 服务中的大小和我要到达设备的大小相同,但我无法在 imageview 中显示。

标签: android imageview base64


【解决方案1】:

首先您必须检查要解码的字符串是否有效并且是否具有要解码的预期值,为此,您可以执行以下操作:

filePath= Environment.getExternalStorageDirectory()
                        + "/SaudiScore/temporary_holder.jpg";
Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
String strBase64=Base64.encodeToString(byteArray, 0);

然后您可以解码刚刚编码的字符串并通过执行以下操作获取图像:

byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
image.setImageBitmap(decodedByte);

【讨论】:

  • 实际上我正在获取编码字符串,发送到 web 服务并返回到设备,那个时候我收到了这个错误。
  • 我之前也遇到过同样的错误,我也遇到了同样的问题,您所要做的就是检查字符串的大小,这实际上是在将其保存到远程数据库之前编码的图像与您检索的字符串的大小相同,一旦您得到完全相同的大小,您将使用上面的代码获得图像。
  • 我正在比较的字符串长度和大小也相同,但我无法在 imageview 中显示。
  • 您在数据库中保存字符串时使用的数据类型是什么?
  • 我没有保存到数据库,直接作为字符串使用。也仅用于发送字符串。我需要保存到数据库中吗?
【解决方案2】:
byte[] decodedString = Base64.decode(mBase64string, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
mImageView.setImageBitmap(decodedByte);

【讨论】:

    【解决方案3】:
    String base = "Base64 string values of some image";
    
    byte[] imageAsBytes = Base64.decode(base.getBytes(), Base64.DEFAULT);
    
    ImageView image = (ImageView) this.findViewById(R.id.imageView1);
    
    image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
    

    试试这个代码。

    【讨论】:

    • 当我使用setImageBitmap 时,我什么也没显示。为什么会这样?
    • 先检查你的base64是否正确。如果这是正确的。那么您可能会在加载视图后尝试添加图像。还要检查您的图像视图 ID。
    【解决方案4】:

    这个方法有帮助:

    private void setExistImage(ImageView imageView, String base64String){
        if (!base64String.isEmpty()) {
            byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
            imageView.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
        }
    }
    

    【讨论】:

      【解决方案5】:

      解码/将base64字符串转换为图像

          imageBytes = Base64.decode(imageString, Base64.DEFAULT);
          Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0,imageBytes.length);
          image.setImageBitmap(decodedImage);
      

      【讨论】:

      • 请解释原提问者做错了什么。
      【解决方案6】:
       byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
      Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
      image.setImageBitmap(decodedByte);
      

      我正在使用上述解决方案。它总是返回错误,如解码的 String is null,IllegalStateException .. 我所做的只是将其包装在 try catch 中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-07
        • 2014-07-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多