Jump to content
jon_bondy

Cannot get correct path to Documents folder on Android using Delphi 10.3

Recommended Posts

When I use this code

path := system.IOUtils.TPath.GetDocumentsPath;

using Delphi 10.3 I get the following value for "path":

/data/user/0/com.embarcadero.<my app name>/files

This makes no sense to me at all.

 

I want to be able to read the contents of the Documents folder, not read and write into a sandboxed folder. All of this was working under 10.2 Tokyo, but no longer works as it once did.

 
I can see the Documents folder using a variety of tools on the Android device. One tool claims the path is '/Documents'. Another claims the path is '/storage/emulated/0/Documents'. Neither path works when used in my Delphi Android application. I can no longer see the files in the Documents folder.
 
I believe that /'storage/emulated/legacy/Documents' worked at one point, but it no longer works (on the same Android device).

Share this post


Link to post

On Android, TPath.GetDocumentsPath is, and has always been, the sandboxed (i.e. app specific) documents folder. If you wish to access the shared documents folder, it is TPath.GetSharedDocumentsPath.

 

Prior to Delphi 10.3, the targetSdkVersion being used in the manifest was 19. In Delphi 10.3 the targetSdkVersion is now 28.

 

From API level 21, applications that need permission for external storage (which is what TPath.GetSharedDocumentsPath points to), need to request permission at runtime. For an example of how to do this, take a look at this demo:

 

https://github.com/Embarcadero/RADStudio10.3Demos/tree/master/Object Pascal/Mobile Snippets/ShareSheet

 

Specifically in the TShareSheetForm.btnTakePhotoClick method in uMain.pas where permissions for external storage are requested.

 

You don't need to request permission for TPath.GetDocumentsPath, so I have no idea why that folder is not working for you. Are you attempting to access files that are being deployed there (i.e. they have entries in Deployment Manager)?

Share this post


Link to post

Dave:

 

Thanks so much for your help!

 

I tried using TPath.GetSharedDocumentsPath, but that resolves to /storage/emulated/0/Documents, which is empty.  No files are visible, even though I know that the Documents folder contains about 100 files.  These files have always been visible in the past.

 

This is the code that I'm currently using to show me the path:

 

  path := TPath.GetSharedDocumentsPath;
  ShowMessage(path);

 

When I say "not working" I mean "my program should be able to see all of the documents in the Documents folder that everyone else can see, and none of the approaches let me see any of the documents that I know are present on the device."  As I said, this software was working fine (the documents were appearing in the list) and now suddenly no documents are visible.  I never needed to deal with permissions before (but I am reading, not writing, to the Documents folder) so I don't know why I would start needing to do so now.

 

The main mystery for me is why it worked and then suddenly did not work.

 

Jon

Share this post


Link to post

You haven't told us what TPath.GetDocumentsPath returns for you in 10.2. I suppose the same as in 10.3.

And I suppose the same for TPath.GetSharedDocumentsPath. Do you see any difference between 10.2 and 10.3?

13 hours ago, jon_bondy said:

The main mystery for me is why it worked and then suddenly did not work.

Because targetSdkVersion is now 28 in your manifest and you need to ask permission for what you want to do. This is an Android OS request. Nothing to do with Delphi.

 

 

  • Like 1

Share this post


Link to post

If you set the proper permission in the manifest you might be able to manually 'activate' that permission in the permission section of the App-settings in Android (instead of asking this via your program).

 

(But for a production program it is best to ask for it in the program itself)

Share this post


Link to post
7 hours ago, Cristian Peța said:

You haven't told us what TPath.GetDocumentsPath returns for you in 10.2. I suppose the same as in 10.3.

And I suppose the same for TPath.GetSharedDocumentsPath. Do you see any difference between 10.2 and 10.3?

Because targetSdkVersion is now 28 in your manifest and you need to ask permission for what you want to do. This is an Android OS request. Nothing to do with Delphi.

 

 

Thanks, Cristian.  I am using the community license for Delphi, so I can no longer use 10.2, and must use 10.3.  I would have been happy to stick with 10.2, but Embarcadero decided otherwise.

 

I will go play with Permissions.  Thanks to all of you for your help.

Share this post


Link to post

Implementing the permissions code did the trick.

 

One thing that puzzled me was that the code without permissions was working on another Android device, but not this one.

 

Thanks everyone for your help!

Share this post


Link to post
15 minutes ago, jon_bondy said:

One thing that puzzled me was that the code without permissions was working on another Android device, but not this one.

Because with older Android version the permission is given during install (if it's asked for by the program). With newer Android the permission needs to be given during runtime (because during install everybody just clicks through without thinking).

  • Like 1

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

×