title: Send Image from Android to Datasnap Restful Server
body: <p>I have an Android app that send data back to a datasnap server using the RESTFul client stuff in XE2.</p>
  
  <p>I have it working fine for sending standard basic data, but part of the app includes storing images the user takes.</p>
  
  <p>I initially tried to use TStream, but that never got back to the server - it just seemed to hang. My current thought is to convert the byte[] of the image to a base64 string and reconvert at the datasnap end.</p>
  
  <p>To convert the image to a base64 string at the Android end, I do the following:</p>
  
  <pre><code>ByteArrayOutputStream stream = new ByteArrayOutputStream();
  image.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
  String encodedString = Base64.encode(stream.toByteArray)
  </code></pre>
  
  <p><code>encodedString</code> then gets sent as a standard Delphi string</p>
  
  <p>at the server end the code to decode is </p>
  
  <pre><code>  function Base64Decode(const EncodedText: string): TBytes;
    var
      DecodedStm: TBytesStream;
      Decoder: TIdDecoderMIME;
    begin
      Decoder := TIdDecoderMIME.Create(nil);
      try
        DecodedStm := TBytesStream.Create;
        try
          Decoder.DecodeBegin(DecodedStm);
          Decoder.Decode(EncodedText);
          Decoder.DecodeEnd;
          Result := DecodedStm.Bytes;
          SetLength(Result, DecodedStm.Size);  // add this line
        finally
          DecodedStm.Free;
        end;
      finally
        Decoder.Free;
      end;
  end;
  </code></pre>
  
  <p>then</p>
  
  <pre><code>var
      Bytes : TBytes;
    image : TJPEGImage;
    stream : TBytesStream;
  begin
    Bytes := Base64Decode(Photo);
    stream := TBytesStream.Create(Bytes);
    image := TJPegImage.Create;
    image.LoadFromStream(stream);
  </code></pre>
  
  <p>This creates an error in the <code>loadFromStream</code> method, basically the jpeg is corrupted. I'm guessing there is something wrong with either then encoding (unlikely), or converting to a delphi string then decoding to a byte[] (likely).</p>
  
  <p>So this is a long winded way to ask if anyone has any suggestions on how to send an image from an Android app to a DataSnap server in Delphi XE2?</p>