I have an Android app that send data back to a datasnap server using the RESTFul client stuff in XE2.
I have it working fine for sending standard basic data, but part of the app includes storing images the user takes.
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.
To convert the image to a base64 string at the Android end, I do the following:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
String encodedString = Base64.encode(stream.toByteArray)
encodedString
then gets sent as a standard Delphi string
at the server end the code to decode is
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;
then
var
Bytes : TBytes;
image : TJPEGImage;
stream : TBytesStream;
begin
Bytes := Base64Decode(Photo);
stream := TBytesStream.Create(Bytes);
image := TJPegImage.Create;
image.LoadFromStream(stream);
This creates an error in the loadFromStream
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).
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?