FMX/Android

FMX - Android 에서 외부 저장 파일 읽고 쓰기

지병철 2021. 4. 13. 17:23

1.  Permission 선택

2. Permission 코딩 하기

uses
	System.Permissions;
var
  _PermissionReadExternalStorage: string;
  _PermissionWriteExternalStorage: string;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
 {$IFDEF ANDROID}  
  _PermissionReadExternalStorage :=
    JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE);
  _PermissionWriteExternalStorage :=
    JStringToString(TJManifest_permission.JavaClass.Write_EXTERNAL_STORAGE);
{$ENDIF}
end;
procedure TfrmMain.FormShow(Sender: TObject);
begin
  PermissionsService.RequestPermissions(
  	[_PermissionReadExternalStorage, _PermissionWriteExternalStorage],
    procedure(const APermissions: TArray<string>;
      const AGrantResults: TArray<TPermissionStatus>)
    begin      
      if (Length(AGrantResults) >= 2) and
        (AGrantResults[0] = TPermissionStatus.Granted) and
        (AGrantResults[1] = TPermissionStatus.Granted) then
      begin
        // do something
      end
      else
        TDialogService.ShowMessage('external storage permission not assigned!');      
    end,
    procedure(const APermissions: TArray<string>;
      const APostRationaleProc: TProc)
    begin
      TDialogService.ShowMessage('external storage premission required!',      
        procedure(const AResult: TModalResult)
        begin
          APostRationaleProc;
        end)
    end); 
end;