Aug.27
Exchange images between Android and PHP using base64 encoding and decoding
Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.
Last day I tried to send a JPG picture from my Android app to a PHP server but got into trouble. Investigating my problem took me an hour so I’d like to share my solution with you.
First Step: Convert the Bitmap to a Base64 string within Java (Android)
I designed a nice framework that uses JSON interfacing to communicate data. So my picture must be converted on my Android device in Base64 string format.
That can be simply done by coding the following:
1 2 3 4 5 6 7 8 9 10 11 |
public static String convert(Bitmap bitmap, int compression) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, compression, baos); byte[] b = baos.toByteArray(); String result = Base64.encodeToString(b, Base64.DEFAULT); //String result = new String(encoded); return result; } |
Steps in between
I sent the base64 string within the JSON request to the server and pull it out back to the same string. I will not discuss that here because there are many sources out there that explain a solution.
Last Step: Get the image back on our PHP server
Now there is the trick. We try to pull out the image data with PHP-code. It seems to be simple.
Just write back the data to disk with a single row:
1 |
file_put_contents(IMAGE_FOLDER . '/myimage.jpg', base64_decode( $base64imageString) ); |
But now I got into trouble! The image file seems to be corrupt. Can’t open it. After comparing the picture on my Android device with the picture on my server I saw that both have the same size but the content was different. Investigating the content I found out that the first part of both files were the same but on some point it starts to differ completely. going back to that point I found out the problem started at a space ‘ ‘ within the base64 string.
Mmm, after some browsing on stackoverflow, I found out that spaces are not OK for PHP. We should make it plusses ‘+’. It turned out that this is a working solution:
Solution
1 2 3 4 5 6 7 8 9 10 11 |
//Replace ' ' with '+'. Spaces are not allowed. $base64ImageString = str_replace(' ', '+', $base64ImageString); //Get a filename $file = IMAGE_FOLDER . '/myimage.jpg'; //Decode the image $decodedImage = base64_decode($base64ImageString); //Write to disk file_put_contents($file, $decodedImage); |
Peter
Awesome article Ton!
Just a small question: Aren’t these 3 lines:
String encoded = Base64.encodeToString(b, Base64.DEFAULT);
String result = new String(encoded);
return result;
redundant, in a sense you could have accomplish the same with:
String encoded = Base64.encodeToString(b, Base64.DEFAULT);
return encoded;
Again, awesome and timesaving article,
Regards,
Peter
Ton Snoei
Hi Peter,
You are right, this row isn’t necessary at all.
Thanks for this comment!