How to convert a Base64 string into a Bitmap image to show it in a ImageView?

后端 未结 6 2166
臣服心动
臣服心动 2020-11-22 10:23

I have a Base64 String that represents a BitMap image.

I need to transform that String into a BitMap image again to use it on a ImageView in my Android app

H

6条回答
  •  清歌不尽
    2020-11-22 11:13

    I've found this easy solution

    To convert from bitmap to Base64 use this method.

    private String convertBitmapToBase64(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream .toByteArray();
        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }
    

    To convert from Base64 to bitmap OR revert.

    private Bitmap convertBase64ToBitmap(String b64) {
        byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
    }
    

提交回复
热议问题