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 :
<img id="img" src="data:image/jpeg;base64,3iVBORw0KGgoAAAANSU...">
My Java encode/decode method
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
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 :
3iVBORw0KGgoAAAANSUhEUgAAAF8AAABfCAYAAACOTBv1AAAABHNCSVQICAgIfAhkiAAAChtJREFU
eJzdnVtIVN8ex3/+R00r/6OWl6Kr0EOF51SioaHY3fB0ke4EmZn2kBGYRQ8VGEKnhNQ6BOXpxV6i
IutAKBidE/HPfy9pRZCYnBSxmJSx0hq1cZ0HmWFf1tp77b0ue+Z8YcHoXuv3/a3P2nvPzN5r7QEI
fRUCwL8AAFko4wBwCwD+6kC+YatFANAL1kBbKW0A4JLVmXBQCYiDbVaWSegfUREO+cYBwDc7DSMj
IyE7OxtSU1MhISEBAAC8Xi90d3dDZ2en3Xw8AJBit3G4aB9Q7pXHjx9Hv379QqzyeDyooKDAytGQ
LJWIBO0Bk07n5+czg6bR8PAwio+PpxmEBNFQRJ924gHAS9rodrtheHjYclCfzwcDAwPg9U6FTkhI
gLS0NMtxBgcHISkpyayaU6dmJvmAsFdVVVWZ7qE+nw9t376d+U315MmT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VCIm7vV6ucGn1dOnT6V5JScnc/HALOz7Ly38
BGVD7Z0iyof/cwOSnZ0tzctufO1zdzB1LEnVeGhoiEuSMoEESnZ2tnAvpYqLi7Xbm63C1w2AUqOj
o0xAFi5cKA3+8+fPhXpRxLClVmWQlJQUlQnLzZY7d+5Ig0+riYkJy7G/fv1qlusiu/BBG6yzs1Nl
FrgHKwqITPjnz5+3FPfixYuq9mvWrNHWYX7OfmAauZXDjBsQHo9+pxXh+cjYol3v+/r1a1w9LrrC
ewBolZiYKA0+bbzU1FSatr/zgg+gmd/DOgBmYn2wUqBUVFQY+vj9fuxcIEbw/+AJPiCViXZOCu0A
FBUVYUHcuHGDC3Bl+fbtG9brwIEDlmPh5vxg6o1yp66QyiwmJkaX0Ny5cw078f79+2DdwcFB7sCV
RanHjx/bjqN91pzBjiZcKkPcA0SN9mKETH8UjFsZHx9HUVFRTDG0Hyd5gbe70ssFAL+0/0SYBWk8
FtU5KQt9stzR38yrYOXHtSWtEMzMzLRp45waGxt14N+8ecMNPC/pDr/m5mbsmxyubqiV2NhYbO6Y
L1DSzvFm0iVFmjMfmC0cioUkQn1xK+VsaBgwSba3t2M71NfX5zhsAOOnjWOuTgaKkM/xrCoFi3sV
QgilpaVJh240e21gYMCoLddvriKETTw6OtpwECYmJmz/hAZNMZsuiLnnqiwh82OUNDoPBiAmJycN
QSA09ZPZZWVltkC7XC504cIFUw+EEPr48aNZvEVSiAnQdzDoWF1dHRUgEaL4pekHDvASItO9lXZP
ZdHOnTtpjpyPsuHI0k+gPHWUlZXp7h1bUUdHB9q8ebOV09UfUkk4KN09AgfLVsF9DWn9B+QDvySj
Y+GmXJh64Cdv2H+CYilOKOh/G/IaeDiFexQAAAAASUVORK5CYII=
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!!!!
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);
E
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.
<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);
}
Related
When i make upload of a image in Android to the server (C #) it generates an error converting the image to byte Array, this happens with some images which is very strange because this error is not always generated, that is, depending the image is correctly converted or not.
The conversion code:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmapPhoto.compress(Bitmap.CompressFormat.JPEG, 50, stream);
final byte[] img_byte = stream.toByteArray();
final String imageString = Base64.encodeToString(img_byte, Base64.DEFAULT);
When receiving in C# I make the following conversion in the string, generating a byte Array and then for the MemoryStream.
byte[] novaFoto = Convert.FromBase64String(NovaFoto.FotoString);
using (MemoryStream stream = new MemoryStream(novaFoto))
{
Image image = Image.FromStream(stream);
}
depending at the image the error appears in either Convert.FromBase64String or Image.FromStream.
Can anyone help me with this? If you have another method i like this.Thanks!!
I'm trying to get the binary form of an image in android to send to a server, but it's not working. When I printed the bytes being created it returns this : [B#cfc2978
public Byte[] getImageBytes(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPG, 50, baos);
byte[] imageBytes = baos.toByteArray();
System.out.println(imageBytes);
return imageBytes;
}
Can Anyone help me find out why this is happening and how to fix it? To clarify, i need to send the binary form of the image to the server to be processed. The testing was done through the emulator.
Hello I am working with android image processing via base 64 encoding and decoding.I need to send an encoded base 64 string to server as a string . When I tried it in android side , I can able to decode it into original image.and my decoding and encoding are as follows
public void base(Bitmap bitmap)
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
toimag(encoded);
String a=encoded.replaceAll("\\s+","");
Log.i("strrr",a);
}
public void toimag(String encodedImage)
{
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
logo.setImageBitmap(decodedByte);
}
But I cant able to get the actual image using http://www.askapache.com/online-tools/base64-image-converter/ . It show only a part of my actual image. This is my base 64 string
iVBORw0KGgoAAAANSUhEUgAAAyAAAAJYCAIAAAAVFBUnAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4
nKS9WbMsyXEm9n0emVVnu/vSDTTQQAMgOCMQHIDkiGM2Gs1QkkmU5kG/QX9QZjKZzYNMD3oZDheJ
Q2rEAUgNFwCNbqBB9N3vOacqw10P7h4RWXXORdOUuKjOk5WVGYsvny/hwf/p6SMAAMxMLE4AGAuA
QgJKAwFAxTCpkEaDAFCjKdTMbDZDHCoASUABoAhJkvktRKT96e/yP0UEANXMf4HCIiIiMpGUKkZC
CKE/QUCSC5Wkt5wksxWlFDOLvpj5K0jSCEDy1d54AALzl0FNVc2MBpJLUQAwMQPV1KrqoqpQq6aq
WmGqWlUXU1UtYIVVUzUutEW1mpmZclbYAqvgIlJV92bV9HozmZkqDFBKBRbYHqpSjKjEAlaKCoxi
xN7UIEpAigorBSxKmOxaH8dPo/qA0CA+zgaS1eJcaAAKSAENPioCnwAfZB9c9WcWkKYk/WlUnxaK
SIFJG+e6+ADGpOe8KKRTQp7QsIiMJNFOlPB59IlK4uzPMbYn+zM1289GeCRNat7PeDIMgGgd6FP8
vWZWSgnKSabwLogIB/KmAQwKEaAoaPBBVDMzW9Bf1w4lNPgGCiMKygRSROayAcAipBgLhDAhWcsS
Y0XCJK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JuWKi2EWhOa3WrDgEfRQNI7VYuImI15OGaAmgpliNB1zCG3+LQaYwzYS5Vj3slo
p1JTdIpCIqTl9AcFBXQDVBxjDdQ8uPYxItf8QAHpfkJaSYEmHUqtp6Ah+hzxUYC6
How can I resolve this issue ? Is there any problem with this encoded string ? Please help. Thanks in advance :)
It appears you are cutting and pasting from the logcat. logcat truncates long messages, so you should not rely on it for copying large pieces of data. You should save the base64 string out to a file and then try your validation using that file.
For Encode image to Base64
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncode = Base64.encodeToString(b,
Base64.DEFAULT);
For decode image
byte[] decodedByte = Base64.decode(imageEncode, 0);
Bitmap newBitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
logo.setImageBitmap(newBitmap);
I have tried several different methods found in the java documentation, as well as several solutions from other SO questions and have successfully gotten a Bitmap to convert to a byte[] and back again.
The problem is that I now need to convert this byte[] to a String, then back to a byte[], then back to a Bitmap again. To recap what I need:
Bitmap -> byte[] -> String -> byte[] -> Bitmap
I know this sounds strange but what I'm trying to accomplish must be done this way. Below is what I have tried, if anyone could point out what I'm doing wrong I'd greatly appreciate it!
Bitmap bitmap = mv.getDrawingCache();
// Convert bitmap to byte[]
ByteArrayOutputStream output = new ByteArrayOutputStream(bitmap.getByteCount());
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
byte[] imageBytes = output.toByteArray();
// Convert byte[] to string
// I have also tried using Base64.encodeToString(imageBytes, 0);
String encodedString = new String(imageBytes);
// Convert string to byte[]
byte[] newImageBytes = encodedString.getBytes();
if (imageBytes == newImageBytes) {
Toast.makeText(SignatureActivity.this, "SUCCESS!!", Toast.LENGTH_SHORT).show();
} else { // Sadly, we always get to this point :(
Toast.makeText(SignatureActivity.this, "BOOO", Toast.LENGTH_SHORT).show();
}
// Convert byte[] back to bitmap
bitmap = BitmapFactory.decodeByteArray(newImageBytes, 0, newImageBytes.length);
Again, going Bitmap -> byte[] -> Bitmap was successful, but adding in the conversion to a String and back is causing the final Bitmap to write an image of 0kb.
The problem is not in the conversion, but how you verify the result. using == to compare two arrays only returns true if they are the same array reference. Since you create a new array with byte[] newImageBytes = encodedString.getBytes(); This will always be false.
See this question for reference.
On another note, if you are going to transfer or use the string in some way, it is probably better to use Base64.encodeToString(imageBytes, Base64.NO_WRAP); to get a string, and get it back with Base64.decode(encodedString, Base64.NO_WRAP).
You can also get the byte array without compressing it, with the copyPixelsToBuffer() method (see this question for an example).
I have an issue about my Application in detail.
- I have a java servlet receive data from mms gateway (MM7 protocol)
I get inputstream (image content , message content ) convert to string
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//String orgin = new String(byteArrayOutputStream.toByteArray(),"UTF-8");
String orgin = Streams.asString(request.getInputStream(), "ISO-8859-1");
Then I substring orgin for image content and convert to base64 and save to image file
but string that I convert to base64 can not save to image because this error
not a jpeg file
I print out string base64 does not start with /9j that mean not jpg format
please suggest or give an example for me
Best reqard
lieang noob noob
sorry for my english :)
This is at least part of your problem:
String orgin = Streams.asString(request.getInputStream(), "ISO-8859-1");
You shouldn't be converting it into a string to start with. It's binary data, right? So read it from the stream as binary data.
Now it sounds like you basically want to get separate "chunks" of that binary data - but converting the data into a string format to start with is not appropriate, unless that binary data really is encoded text.
I think, this might help: http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx
Encoding of image is very simple.
Encoding Source:
Bitmap bm = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArray = baos.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Decoding Source:
byte[] decodedString;
decodedString = Base64.decode(picture, Base64.DEFAULT);
imageView1.setImageBitmap( BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length));
Use the below code the for the String to Image
here "origin" is a String
import org.apache.commons.codec.binary.Base64;
byte[] imgByteArray = Base64.decodeBase64(origin);
FileOutputStream imgOutFile = new FileOutputStream("C:\\Workspaces\\String_To_Image.jpg");
imgOutFile.write(imgByteArray);
imgOutFile.close();