1

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?

2
  • Are you sure responseString and thus data are not null? Commented Feb 3, 2011 at 16:55
  • Of course, i'm sure. data and responseString are not null. Commented Feb 4, 2011 at 7:51

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.