Android File Read / Write Permissions

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:

LHasAccess := TJEnvironment.JavaClass.isExternalStorageManager;

What is the current correct way to gain R/W access?

1 Like

Also, I’ve never seen any permission prompts on the phone when running my app (USB debug mode). Should I ?

1 Like

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:

When I open it in the Google Docs viewer it looks like this:

Which is correct.

1 Like

Hi David

Ian has given me something to think about as I gave up on writing Android files to share folders some time ago.

I also have a note to follow up on Chester Wilson’s post

which might help you too

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.

In case it helps I have no trouble reading and writing to/from an ini file using the following file name

{$IFDEF Android}
    IniFileName := System.IOUtils.TPath.Combine(
     System.IOUtils.TPath.GetHomePath,'<ApplicationName>.ini');
{$ELSE}

But

  1. I cannot easily access the file outside the application so the application must be used to set the values in the ini.
  2. When you use a -cleaninstall the ini file and all files in “Home Path” are also deleted

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.

1 Like

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.

1 Like

Is it possible you do not have the “read external storage” checked?

This is probably required. FWIW, my test app is running on a Samsung Galaxy S25 Ultra with the latest version of Android.

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.

Chester Wilson.

1 Like

Do you have a link to the post somewhere?

Ian:

Link is:

https://59b7770ca3fb18e4e90d-52e2682744779750b5103bbecf998085.ssl.cf2.rackcdn.com/FileAccess.zip

(it’s on the post)

1 Like

Perfect, thanks! :grin::+1:

Part of my issue was that the files I was using had spaces in the names, which appears to be not supported. See this thread for more details:

I have done a bit of work since - put in recording photos and sound, if that may be useful to you.

I have added recording photos and sound in the last few weeks - see if it helps.

Sorry, photos and sound where?

To his original link, i.e.

https://59b7770ca3fb18e4e90d-52e2682744779750b5103bbecf998085.ssl.cf2.rackcdn.com/FileAccess.zip

1 Like