Jump to content
Eric Dudley

TDirectory.GetFiles() is not working in Delphi 11

Recommended Posts

In Delphi 10.4, I was able to get a listing of files in an Android 11 device's shared storage. In Delphi 11.2, with necessary permission set (either programmatically or on the device), TDirectory.GetFiles() is returning an empty list (ie. zero entries).

TDirectory.GetDirectories() is working correctly, and using TFile.Exists() with a filename which I know to be present returns true. But, if I try to open that file with, for instance, TFile.ReadAllText(), access is denied. Any suggestions?

This is an example program which fails in Delphi 11.2:

 

unit test11main;

interface
uses
  System.Types,System.Classes,system.sysUtils,system.IOUtils,system.Permissions,
 {$IFDEF ANDROID}
   Androidapi.Helpers,
   Androidapi.JNI.JavaTypes,
   Androidapi.JNI.Os,
 {$ENDIF}
  FMX.Types,FMX.Forms,FMX.Dialogs;

type
  TForm1 = class(TForm)
   procedure FormCreate(Sender: TObject);
  private
   fOK:boolean;
   procedure PermissionsResult(Sender: TObject;
      const APermissions: TClassicStringDynArray;
      const AGrantResults: TClassicPermissionStatusDynArray);
  end;

var
  Form1: TForm1;

implementation
{$R *.fmx}

procedure Tform1.PermissionsResult(Sender: TObject;
  const APermissions: TClassicStringDynArray;
  const AGrantResults: TClassicPermissionStatusDynArray);
 var
  n:integer;
 begin
  if length(AGrantResults)>0 then
   for n:=0 to length(AGrantResults)-1 do
    if not (AGrantResults[n] = TPermissionStatus.Granted) then fOK:=false;
 end;

procedure TForm1.FormCreate(Sender: TObject);
 var
  DataDir:string;
  p:Tarray<string>;
  LList: TStringDynArray;
 begin
  fOK:=true;
  { if I omit the next two lines and manually set permissions on the
    Android device it still does not work}

  p:=[JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE),
        JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE)];
  PermissionsService.RequestPermissions(p,PermissionsResult,nil);

  if fOK then
   begin
    DataDir := TPath.GetSharedDocumentsPath;
showmessage(DataDir); //correctly displays /storage/emulated/0/Documents
    Llist:=Tdirectory.GetFiles(DataDir);
showmessage(inttostr(length(Llist)));  //returns zero when run in Delphi 11.2
   end else showmessage('permission refused');
 end;

end.

 

Edited by Eric Dudley

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×