// ein Beispielcode mit Permissions und Verzeichnis anlegen in D11.3
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,FMX.DialogService,system.permissions,
FMX.Controls.Presentation, FMX.StdCtrls;
type
TForm1 =
class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private-Deklarationen }
FRead,
FWrite:
string;
procedure SetPermissions;
procedure MakeReadWritePermissionRequestResult(Sender:TObject;
const APermissions: TClassicStringDynArray;
const AGrantResults: TClassicPermissionStatusDynArray);
procedure DisplayRationale(Sender: TObject;
const APermissions: TClassicStringDynArray;
const APostRationaleProc: TProc);
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
zugriffErlaubt :boolean = false;
implementation
uses Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os;
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
// dies ist für mich der wichtigste Teil
var basisdir,dir,Verzeichnis:
string;
begin
basisdir := '
/storage/emulated/0/';
dir := '
testdir';
Verzeichnis := basisdir+dir;
showmessage('
Zugriff erlaubt= '+BoolToStr(zugriffErlaubt,true)+'
auf '+Verzeichnis);
// if zugriffErlaubt then
// if not System.SysUtils.directoryexists(basisdir+dir) then
System.SysUtils.ForceDirectories(Verzeichnis);
// funzt leider nicht. bis D10.4 ging's aber so. Auch mit Android 13
end;
procedure TForm1.DisplayRationale(Sender: TObject;
const APermissions: TClassicStringDynArray;
const APostRationaleProc: TProc);
begin
TDialogService.ShowMessage('
Berechtigung erforderlich',
procedure(
const AResult: TModalResult)
begin
APostRationaleProc;
end)
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
setpermissions;
end;
procedure TForm1.SetPermissions;
begin
if CompilerVersion <= 32
then exit;
FRead := JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE);
FWrite := JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE);
PermissionsService.RequestPermissions([FRead,FWrite],MakeReadWritePermissionRequestResult, DisplayRationale);
end;
procedure TForm1.MakeReadWritePermissionRequestResult(Sender:TObject;
const APermissions: TClassicStringDynArray;
const AGrantResults: TClassicPermissionStatusDynArray);
begin
if (Length(AGrantResults) = 2)
and (AGrantResults[0] = TPermissionStatus.Granted)
and (AGrantResults[1] = TPermissionStatus.Granted)
then zugriffErlaubt := True
else zugriffErlaubt := False;
end;
end.