//In this example the app registers several photos that need to be exported.
// procedure export(arqname:string);
// procedure AndroidModule1ActivityResult(Sender: TObject;requestCode: integer; resultCode: TAndroidResult; intentData: jObject);
// function StreamFileSmart(const FilePath: string; const Uri: jObject): Boolean;
// var zipnam :string; //Global var
// call export
// zipnam:='fotos.zip' ; export (zipnam);
// Here a .zip file (zipnam) is created with all the photos registered in the app and we invoke the file creation to obtain the uri
procedure TAndroidModule1.export(arqname:string);
var zip: TZipper;i: Integer;relativefn,tmp: String;
begin
L := TStringList.Create;FindAllFiles(L,GetEnvironmentDirectoryPath(dirDCIM), '*.jpg', true);
zip := TZipper.Create;
try
zip.Filename := arqname;
try
for i:=0 to L.Count-1 do begin
if L[i] ='' then break;
relativefn := CreateRelativePath(L[i],GetEnvironmentDirectoryPath(dirDownloads));
zip.Entries.AddFileEntry ( SysToUTF8(L[i]),extractfilename( l[i] ));
end;
finally
L.Free;
end;
zip.SaveToFile(GetEnvironmentDirectoryPath(dirDownloads)+'/'+arqname);
Self.RequestCreateFile(Self.GetEnvironmentDirectoryPath(dirDownloads),'application/zip',arqname, 114);
finally
zip.Free;
end;
end;
// This is where we get the .zip file's uri and call the function to write the content.
procedure TAndroidModule1.AndroidModule1ActivityResult(Sender: TObject;
requestCode: integer; resultCode: TAndroidResult; intentData: jObject);
begin
treeUri:= IntentManager1.GetDataUri(intentData);
if requestCode = 114 then //create file
begin
Self.CopyFile(Self.GetEnvironmentDirectoryPath(dirDatabase)+'/'+zipnam,Self.GetEnvironmentDirectoryPath(dirDownloads)+'/'+zipnam );
StreamFileSmart(GetEnvironmentDirectoryPath(dirDownloads)+'/'+zipnam,treeUri);
end;
end;
// Here's the function that uses AppendBytesToUri for any file size. It sends a maximum of 200 MB and adjusts the array size if necessary.
function TAndroidModule1.StreamFileSmart(const FilePath: string; const Uri: jObject): Boolean;
const
MAX_CHUNK = 200 * 1024 * 1024;
var
fs: TFileStream;
buffer: array of byte;
jBuffer: TDynArrayOfJByte;
bytesRead, chunkSize: Integer;
begin
Result := False;
if not FileExists(FilePath) then Exit;
try
fs := TFileStream.Create(FilePath, fmOpenRead or fmShareDenyWrite);
try
if fs.Size <= MAX_CHUNK then
chunkSize := fs.Size
else
chunkSize := MAX_CHUNK;
SetLength(buffer, chunkSize);
SetLength(jBuffer, chunkSize);
repeat
bytesRead := fs.Read(buffer[0], chunkSize);
if bytesRead > 0 then
begin
Move(buffer[0], jBuffer[0], bytesRead);
SetLength(jBuffer, bytesRead);
AppendBytesToUri( jBuffer, Uri);
end;
until bytesRead < chunkSize;
Result := True;
finally
fs.Free;
end;
except
on E: Exception do
begin
// log error ou message
Result := False;
end;
end;
end;