XE2 の RESTFul クライアントを使用して datasnap サーバーにデータを送信する Android アプリがあります。
標準の基本データの送信には問題なく機能していますが、アプリの一部には、ユーザーが撮影した画像の保存が含まれています。
最初に TStream を使用しようとしましたが、サーバーに戻ることはありませんでした。ハングしているように見えました。私の現在の考えは、画像の byte[] を base64 文字列に変換し、datasnap の最後で再変換することです。
Android 側で画像を base64 文字列に変換するには、次のようにします。
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
String encodedString = Base64.encode(stream.toByteArray)
encodedString
次に、標準の Delphi 文字列として送信されます
サーバー側でデコードするコードは
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;
それから
var
Bytes : TBytes;
image : TJPEGImage;
stream : TBytesStream;
begin
Bytes := Base64Decode(Photo);
stream := TBytesStream.Create(Bytes);
image := TJPegImage.Create;
image.LoadFromStream(stream);
これにより、loadFromStream
メソッドにエラーが発生します。基本的には jpeg が破損しています。エンコーディング (可能性は低い) か、delphi 文字列に変換してから byte[] にデコードする (可能性が高い) のいずれかに問題があると思います。
Android アプリから Delphi XE2 の DataSnap サーバーに画像を送信する方法について何か提案があるかどうかを尋ねるには、これは長い道のりです。