Reputation: 2307
I am attempting to create a Delphi XE5 Android Datasnap application (regular, not REST) that uploads pics.
See the code below. The code works! The image taken by the camera on the Android phone gets transferred to the server and saved. The problems are:
I'm confused. Where is all this automatic conversion taking place?
The client code looks like this
procedure TfrmMain.TakePhotoFromCamerAction1FinishTaking(Image: TBitMap)
var
ImageStream: TMemoryStream;
Bytes: Integer;
FileName: String;
begin
ImageStream := TMemoryStream.Create;
try
Image.SaveToStream(ImageStream);
ImageStream.Position := 0;
FileName := 'Image001.bmp';
Bytes := ClientModule1.ServerMethods1Client.UploadImage(FileName, ImageStream);
if Bytes = -1 then
raise exception.create('Image transfer failed!')
finally
ImageStream.Free
end;
end;
The method on the server looks like this
function TServerMethods1.UploadImage(FileName: String; Stream: TStream): Integer;
const
BufSize = $F000;
var
Mem: TMemoryStream;
BytesRead: Integer;
Buffer: PByte;
begin
frmMain.LogMessage('Upload image ' + FileName);
try
Mem := TMemoryStream.Create;
GetMem(Buffer, BufSize);
try
repeat
BytesRead := Stream.Read(Buffer^, BufSize);
if BytesRead > 0 then
Mem.WriteBuffer(Buffer^, BytesRead);
until BytesRead < BufSize;
// here, replace with DB update
// for now, save to file
Mem.Seek(0, TseekOrigin.soBeginning); // necessary?
Mem.SaveToFile(ExtractFilePath(Application.ExeName) + FileName);
// ============================
Result := 1; // Mem.Size doesn't work...
finally
FreeMem(Buffer, BufSize);
Mem.Free;
end;
except
Result := -1
end;
end;
Upvotes: 1
Views: 2182