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:

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:

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

Tags:
Deel dit Verhaal:
  • facebook
  • twitter
  • gplus

Over Ton Snoei

Een enthousiaste ontwikkelaar in de allerbreedste zin van het woord.

Het aansturen maar vooral coachen van mensen geeft me veel plezier en een leuke uitdaging. Ik houdt me graag bezig met het bouwen van software maar ook met alle anderen dingen die komen kijken bij het vak dat IT heet.

Met als achtergrond ontwikkelaar heb ik veel ervaring met C# .NET en alles wat daarbij komt kijken. Deze kennis komt nog regelmatig goed van pas. Mijn liefde voor ontwikkeling van software is nooit verdwenen en heb mij daarom in het afgelopen jaar meer verdiept in Java, Android, iOS/iPhone/iPad (Monotouch/Xamarin), PHP en Unity 3D development. Ik noem dat altijd maar: "Lego-spelen voor grote mensen" ;-)

Kortom van de grote lijnen naar projecten tot hands-on problem solven.

Reacties(2)

  1. Peter
    1993 days geleden

    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
      1989 days geleden

      Hi Peter,

      You are right, this row isn’t necessary at all.

      Thanks for this comment!

Laat een reactie achter

Comment