Ramoz
Ramoz

Reputation: 21

Android: get image from base64binary format

I use web service to get image. The service response contains image in base64Binary format. I try to decode response data with Base64.decode() (http://iharder.sourceforge.net/current/java/base64/). See my code below:

 byte[] data = Base64.decode(responseString);
 Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
 imageView.setImageBitmap(bmp);

decodeByteArray always return null.

I try to save data in .png file. I can open this file on my PC and in the Android File Manager application. But preview activity of File Manager couldn't open this file.

Then i try to parse this data using .NET client with Convert.Base64() method. And this image have been processing successfully. Then i compare byte array in image created with android client and .NET client. The differences were in sign of bytes. .NET uses unsigned bytes but Java use only signed bytes. Is this is a reason of my problem?

Is anybody have the same problem in decoding of base64Binary?

Upvotes: 1

Views: 2549

Answers (1)

Iulia
Iulia

Reputation: 31

Here is one solution, and for me is working (knowing that the format in which the image comes from the server through the web service is base64binary)

decodedIcon[] = null;
byte[] bb = (resposeString).getBytes("utf-8");
decodedIcon = Base64.decodeBase64(bb);

Bitmap bitmap = BitmapFactory.decodeByteArray(decodedIcon, 0,
decodedIcon.length);

//then you get the image view and setImageBitmap(bitmap)

PS:

Base64.decodeBase64 comes from the library org.apache.commons.codec.binary.Base64; You should have commons-codec-1.3.jar included in the assets folder

the version doesn't have to be 1.3

Thanks to one of my friends for this hint.

Upvotes: 2

Related Questions