I develop a custom messenger application which sends message from client(Android) to another client(web). My problem is when I send image file, I encoded it to Base64
string and then send it to another client. When the recipient client is Android, the app successfully decoded the Base64
String to bitmap image. But when the recipient client is web app, the web application failed to decode and display it using :
我开发了一个定制的messenger应用程序,它从客户端(Android)向另一个客户端(web)发送消息。我的问题是,当我发送图像文件时,我将它编码到Base64字符串,然后发送给另一个客户端。当接收端为Android时,应用程序成功将Base64字符串解码为位图图像。但当接收方客户端是web应用时,web应用程序无法解码并显示:
<img id="img" src="data:image/jpeg;base64,3iVBORw0KGgoAAAANSU...">
My Java encode/decode method
我的Java编码/解码方法
public static Bitmap base64StringToBitmap(String base64) {
byte[] bytes = Base64.decode(base64, Base64.URL_SAFE);
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
public static String bitmapToBase64String(Bitmap bmp, Bitmap.CompressFormat format, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(format, quality, baos);
byte[] bytes = baos.toByteArray();
return Base64.encodeToString(bytes, Base64.URL_SAFE);
}
I've tried to change the encoding method to Base64.DEFAULT
, but still the Base64
String cannot be displayed in HTML using data:image/jpeg;base64
. Is there any differences between Android Base64
string and Base64
string used to display image in HTML? Please suggest me the solution. Thanks
我尝试将编码方法改为Base64。默认,但是仍然不能使用data:image/jpeg; Base64在HTML中显示Base64字符串。Android Base64字符串和用于在HTML中显示图像的Base64字符串之间有什么区别吗?请给我建议解决办法。谢谢
EDIT :
编辑:
This is the base64 string example generated from my Android application, with padding. I failed to display it on html/web, although it can be displayed fine on Android :
这是我的Android应用程序生成的base64字符串示例,带有填充。我没有在html/web上显示它,虽然在Android上可以很好地显示:
3iVBORw0KGgoAAAANSUhEUgAAAF8AAABfCAYAAACOTBv1AAAABHNCSVQICAgIfAhkiAAAChtJREFU
eJzdnVtIVN8ex3/+R00r/6OWl6Kr0EOF51SioaHY3fB0ke4EmZn2kBGYRQ8VGEKnhNQ6BOXpxV6i
IutAKBidE/HPfy9pRZCYnBSxmJSx0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ltkC7XC504cIFUw+EEPr48aNZvEVSiAnQdzDoWF1dHRUgEaL4pekHDvASItO9lXZP
ZdHOnTtpjpyPsuHI0k+gPHWUlZXp7h1bUUdHB9q8ebOV09UfUkk4KN09AgfLVsF9DWn9B+QDvySj
Y+GmXJh64Cdv2H+CYilOKOh/G/IaeDiFexQAAAAASUVORK5CYII=
2 个解决方案
#1
3
I had a similar issue where I wanted to convert an Android generated BASE64 string to Binary with Javascript and atob function was keep giving me errors. My first guess was like yours to use URL_SAFE
instead of DEFAULT
, but none of them worked, then I figure it out that I need to use Base64.NO_WRAP
method to get it working. I tested Base64.NO_WRAP
and displaying inline image and IT WORKED!!!!
我也遇到过类似的问题,我想用Javascript把Android生成的BASE64字符串转换成二进制,atob函数不断地给我出错。我的第一个猜测是使用URL_SAFE而不是默认值,但它们都不起作用,然后我发现我需要使用Base64。NO_WRAP方法使其工作。我测试了Base64。NO_WRAP和显示内联图像,它工作了!!!
Hope this save you some nerves, because I did had some till I figure it out how to do it.
希望这不会让你太紧张,因为在我弄明白怎么做之前,我确实有过一些紧张。
So to give the nice answer, change: return Base64.encodeToString(bytes, Base64.URL_SAFE);
to this: return Base64.encodeToString(bytes, Base64.NO_WRAP);
所以要给出漂亮的答案,改变:返回Base64。encodeToString(字节,Base64.URL_SAFE);:返回Base64。encodeToString(字节,Base64.NO_WRAP);
E
E
#2
0
can you try with this for me it's working fine one two change only Base64.DEFAULT insted of URL_SAFE and image tag alt. just check it.
你能帮我试试这个吗?它很好用。URL_SAFE和image标签的默认insted。
<img height=700px src='data:image/jpg;base64,"+encodeString+"' alt='Embeded Image'/>
public static String bitmapToBase64String(Bitmap bmp, Bitmap.CompressFormat format, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(format, quality, baos);
byte[] bytes = baos.toByteArray();
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
#1
3
I had a similar issue where I wanted to convert an Android generated BASE64 string to Binary with Javascript and atob function was keep giving me errors. My first guess was like yours to use URL_SAFE
instead of DEFAULT
, but none of them worked, then I figure it out that I need to use Base64.NO_WRAP
method to get it working. I tested Base64.NO_WRAP
and displaying inline image and IT WORKED!!!!
我也遇到过类似的问题,我想用Javascript把Android生成的BASE64字符串转换成二进制,atob函数不断地给我出错。我的第一个猜测是使用URL_SAFE而不是默认值,但它们都不起作用,然后我发现我需要使用Base64。NO_WRAP方法使其工作。我测试了Base64。NO_WRAP和显示内联图像,它工作了!!!
Hope this save you some nerves, because I did had some till I figure it out how to do it.
希望这不会让你太紧张,因为在我弄明白怎么做之前,我确实有过一些紧张。
So to give the nice answer, change: return Base64.encodeToString(bytes, Base64.URL_SAFE);
to this: return Base64.encodeToString(bytes, Base64.NO_WRAP);
所以要给出漂亮的答案,改变:返回Base64。encodeToString(字节,Base64.URL_SAFE);:返回Base64。encodeToString(字节,Base64.NO_WRAP);
E
E
#2
0
can you try with this for me it's working fine one two change only Base64.DEFAULT insted of URL_SAFE and image tag alt. just check it.
你能帮我试试这个吗?它很好用。URL_SAFE和image标签的默认insted。
<img height=700px src='data:image/jpg;base64,"+encodeString+"' alt='Embeded Image'/>
public static String bitmapToBase64String(Bitmap bmp, Bitmap.CompressFormat format, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(format, quality, baos);
byte[] bytes = baos.toByteArray();
return Base64.encodeToString(bytes, Base64.DEFAULT);
}