【问题标题】:Cannot decode Base64 String to Bitmap [duplicate]无法将 Base64 字符串解码为位图 [重复]
【发布时间】:2026-02-01 10:40:02
【问题描述】:

我正在尝试将 Base64 字符串图像解码为 Bitmap,但它总是返回 null

这是我的代码:

 public static Bitmap getBitmap(String encode) {

        Bitmap bm = null;
        try { 
            byte[] decodedString = Base64.decode(encode, Base64.DEFAULT);
            bm = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

        } catch (Exception e) { 
            Log.e(TAG,e.getMessage());
            return bm;
        }

        return bm;
    }

【问题讨论】:

  • 发布你的代码会有帮助
  • 这是我的代码: public static Bitmap getBitmap(String encode) { Bitmap bm = null;尝试 { byte[] decodedString = Base64.decode(encode, Base64.DEFAULT); bm = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); } catch (Exception e) { Log.e(TAG,e.getMessage());返回 bm; } 返回 bm; }

标签: java android base64


【解决方案1】:

对我来说,这段代码有效:

Bitmap bmp = BitmapFactory.decodeByteArray(Base64.decode(B64.BASEFILE, Base64.DEFAULT), 0, Base64.decode(B64.BASEFILE, Base64.DEFAULT).length);
ImageView iw = findViewById(R.id.imageView);
iw.setImageBitmap(bmp);

我在 API 22 上编译它。

【讨论】:

  • 和我的功能一样