2

I am receiving a Base64 blob response from my web service. I want to create one image and set it to Imageview in my activity. Here's the code snippet. Apologies for pasting response.

bs64img - Base64 string response code -

byte[] imageBytes=Base64.decode(bs64img, Base64.DEFAULT);
InputStream is = new ByteArrayInputStream(imageBytes);
Bitmap image=BitmapFactory.decodeStream(is);
ImageView i=(ImageView)findViewById(R.id.imageView1);
i.setImageBitmap(image);

Please help me on this.

Lucifer
  • 29,392
  • 25
  • 90
  • 143

2 Answers2

1

You could try:

byte[] imgBytesData = android.util.Base64.decode(yourBase64String);
Bitmap bitmap = BitmapFactory.decodeByteArray(imgBytesData, 0, imgBytesData.length);
... then setting your ImageView or saving on FileSystem or ...
alrama
  • 618
  • 11
  • 17
0

In general, embedded images are given as

<img src="data:image/png;base64,iVBORw0KGg...">

That is, all bytes of the file are given. So try:

byte[] imageBytes=Base64.decode(bs64img, Base64.DEFAULT);
InputStream is = new ByteArrayInputStream(imageBytes);
BufferedImage img = ImageIO.read(is);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138