0

I am getting a base 64string from my api which Looks something like this:

"data:image/png;base64,iVBORw0KGgoAAAANSUhEUg.....KUfsaX4AAAAASUVORK5CYII="

I Need to put the Image on a imageview. I tried following to place it on an imageview:

@BindView(R.id.zeichnung)
ImageView drawable1;

            try {
                String result = response.body().string(); // result holds the Image above

                byte[] decodedString = Base64.decode(result, Base64.DEFAULT);
                Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                //Insert Image
                drawable1.setImageBitmap(decodedByte);
            } catch (Exception e) {
                Toasty.error(VideoChatActivity.this, "ERROR.", Toast.LENGTH_SHORT, true).show();
            }

But it keeps throwing me the catch toast.

Jaan
  • 251
  • 1
  • 6
  • 16
  • *keeps throwing me the catch stuff*? Well add that stuff in question. Add the stacktrace from log . – ADM Mar 23 '18 at 15:20
  • You can find your answers here : https://googleweblight.com/i?u=https://stackoverflow.com/questions/4837110/how-to-convert-a-base64-string-into-a-bitmap-image-to-show-it-in-a-imageview&hl=en-IN – Akash Khatri Mar 23 '18 at 15:43

1 Answers1

0

You will have to remove data:image/png;base64, from the image string before converting to bitmap like this,

            String result = response.body().string(); // result holds the Image above

            byte[] decodedString = Base64.decode(result, Base64.DEFAULT);
            final String pureBase64Encoded = decodedString.substring(decodedString.indexOf(",")  + 1);
            Bitmap decodedByte = BitmapFactory.decodeByteArray(pureBase64Encoded, 0, pureBase64Encoded.length());
            //Insert Image
            drawable1.setImageBitmap(decodedByte);
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44