Android: get image from base64binary format

ぐ巨炮叔叔 提交于 2020-01-06 02:29:13

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4888746/android-get-image-from-base64binary-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!