I have an Android App , where I save the image and convert it to PNG .
Stream := TMemoryStream.Create;
try
Stream.Position:=0;
Surf := TBitmapSurface.Create;
try
Surf.Assign(imgObject.Bitmap);
// convert BMP to PNG
TBitmapCodecManager.SaveToStream(Stream,Surf,'.png');
finally
Surf.Free;
end;
// here do the save to file which we later upload
FileDate:=FormatDateTime('yyyymmddhhnnsszzz',now);
IniWrite(Main.FilePath+'/temp.BatchData','DATA','LocationID',ID);
IniWrite(Main.FilePath+'/temp.BatchData','DATA','BatchNumber',edtBatch.Text);
IniWrite(Main.FilePath+'/temp.BatchData','DATA','Type','BatchTrack');
// write the image
Stream.SaveToFile(Main.FilePath+'/'+FileDate+'.img');
RenameFile(Main.FilePath+'/temp.BatchData',Main.FilePath+'/'+FileDate+'.out');
finally
Stream.Free;
end;
Since I had to make sure the Image is saved correctly I created a test procedure for it :
procedure TMain.Button1Click(Sender: TObject);
var tmpFiles : TStringDynArray;
Files : TStringDynArray;
ImageFile : string;
Image : TMemoryStream;
surf : TBitmapSurface;
Bitmap : TBitmap;
begin
Files := TDirectory.GetFiles(FilePath+'/','*.out');
ImageFile := ChangeFileExt(Files[0],'.img');
Image := TMemoryStream.Create;
Image.LoadFromFile(ImageFile);
Image.Position:=0;
surf := TBitmapSurface.Create;
TBitmapCodecManager.LoadFromStream(Image,surf);
Image1.Bitmap.Assign(surf);
end;
The Image loads correctly on the Android device.
I have following function to upload data via DataSnap :
LocationID := inireadInt(Files[0],'DATA','LocationID');
BatchName := inireadStr(Files[0],'DATA','BatchNumber');
ImageFile := ChangeFileExt(Files[0],'.img');
Image := TMemoryStream.Create;
Image.LoadFromFile(ImageFile);
Image.Position:=0;
if FileType = 'BatchTrack' then
begin
if Temp.BATCH_TRACK(LocationID,BatchName,Image) = 1 then
begin
System.SysUtils.DeleteFile(Files[0]);
end;
end;
The BATCH_TRACK function itself on the client looks like this :
function TServerMethods1Client.BATCH_TRACK(HeadID: integer; BatchNumber: string;
Image: TMemoryStream): integer;
begin
if FBATCH_TRACKCommand = nil then
begin
FBATCH_TRACKCommand := FDBXConnection.CreateCommand;
FBATCH_TRACKCommand.CommandType := TDBXCommandTypes.DSServerMethod;
FBATCH_TRACKCommand.Text := 'TServerMethods1.BATCH_TRACK';
FBATCH_TRACKCommand.Prepare;
end;
FBATCH_TRACKCommand.Parameters[0].Value.SetInt32(HeadID);
FBATCH_TRACKCommand.Parameters[1].Value.SetWideString(BatchNumber);
FBATCH_TRACKCommand.Parameters[2].Value.SetStream(Image,FInstanceOwner);
FBATCH_TRACKCommand.ExecuteUpdate;
Result := FBATCH_TRACKCommand.Parameters[3].Value.GetInt32;
end;
on the Server itself I now have a empty function :
function TServerMethods1.BATCH_TRACK(HeadID : integer; Batchnumber : string; Image : TMemoryStream) : integer;
var MS : TMemoryStream;
begin
try
MS := TMemoryStream.Create;
MS.LoadFromStream(Image);
MS.Position:=0;
Result := 1;
except
Result := -1;
end;
end;
I get a Invalid Typecast error when debugging the client .
Please help.
UPDATE 1
I have changed both Server and Client from TMemoryStream to TStream. This works but only for very small files.
If I try to send an image for example 75Kbyte . I get a Unable to Expand Memory Stream due to lack of memory .