I’m new to FMX and Android development, so bear with me (I have Delphi 12.3). I’m trying to list the files in some of the shared directories, but the file count always returns zero.
I think where I’m having trouble with is requesting file read / write permissions. Based on some examples online, I used:
procedure TfmMain.RequestPermissions;
var
FPermissionRead, FPermissionWrite: string;
begin
FPermissionRead := JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE);
FPermissionWrite := JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE);
PermissionsService.RequestPermissions([FPermissionRead,FPermissionWrite], nil);
end;
To check success I used this but it returns false:
A couple of things here - the permissions are a little tricky but the IDE controls most of them automatically through the “Uses permissions” section on the project properties page.
Second, I’m not sure what code you’re using to enumerate the files, but this code will work:
procedure TForm1.SharedFileWriteTest;
var
LPath: string;
FS: TFileStream;
procedure AddIt(const AComment, AItem: string);
begin
ListBox1.Items.Add(AComment + AItem);
end;
procedure ListIt(const AHeading: string);
begin
AddIt(AHeading, '');
var TheFiles := TDirectory.GetFiles(TPath.GetSharedDocumentsPath);
for var LFile in TheFiles do
AddIt('File=', LFile);
end;
begin
AddIt('Shared Docspath=', TPath.GetSharedDocumentsPath);
ListIt('Before');
var ThePath: string := TPath.Combine(TPath.GetSharedDocumentsPath, 'mytest.txt');
try
FS := TFileStream.Create(ThePath, fmCreate or fmOpenWrite);
var sTime: string := DateTimeToStr(Now);
FS.Write(PChar(sTime)^, Length(sTime) * SizeOf(Char));
FS.Free;
except on E: Exception do
AddIt('ERROR=', E.Message);
end;
ListIt('After');
end;
After running this on my Samsung Galaxy I see the following file (which wasn’t there before) in the Android Files app - so it’s definitely accessible to other apps:
Thanks guys. I’ll have another go at this project today. I mostly want read access for audio files, but will have to read and write the configuration file.
Ian, your example does work but the listing in the app only shows files that the app created. It doesn’t show any of the files that I copied into that folder from my PC, so it looks like the read permissions are not quite right.
EDIT: It’s the same issue when using the shared or app download folders too.
OK, I’m onto something. It looks like more granular permissions are required for Android 15 on my Samsung A 55 phone. If I request the permission READ_MEDIA_AUDIO I get a prompt the first time it’s run. Once I say yes to that the media files (that I loaded via Windows) do show up in my app. It still doesn’t show the text file so perhaps there’s another permission for that.
I have thew following enabled:
Internet ← not sure if I’ll need that for WLAN access (UDP)
Read External Storage
Read Media Audio
Read Phone State ← probably don’t need that
Write External Storage
David, have a look at my recent post. I gave up android work for a couple of years due to the mess they made with the file system on it, and recently kicked myself to finally get sonmething working. It took two months of solid work, a huge amount of hair-tearing, and masses of help from Dave Nottage! I have put it up so that others may get away without the same trouble, so I hope it may be useful to you.