2

I'm pretty new to Android development. I want to develop an App, that is able to save a text file on the internal storage (in the App's private folder or something like that).

I have following code:

    procedure TfrmMain.Button1Click(Sender: TObject);
    var
      tstrTmp: TStringList;
      sFn: String;
    begin
      tstrTmp := TStringList.Create;
      tstrTmp.Text := 'Test';
      sFn := Format( '%s/Test.txt', [GetHomePath ]);
      sFn := System.IOUtils.TPath.GetDocumentsPath + System.SysUtils.PathDelim + 'Test.txt';
      sFn := TPath.Combine( System.IOUtils.TPath.GetDocumentsPath , 'Test.txt' );
      tstrTmp.SaveToFile( sFn );
      tstrTmp.Free;
      showmessage( sFn );
    end;

When I run the App and click the button, it is showing me: '/data/user/0/com.embarcadero.[MyAppName]/files/Test.txt'. So, I would assume that the file was physically written to that location.

But when I connect my device to my pc, I can't even find this folder. I also searched on the external storage but I was not able to find this folder or file (Test.txt).

I did found a folder on my internal storage : /Android/data/com.embarcadero.[MyAppName]/files, but no file in there.

so, I'm a bit stuck here, because I would assume the file is saved somewhere. But I'm not able to find it.

Am I doing something wrong here? Thanks for your feedback.

Best Regards,

Wim.

9
  • Just a quick guess: Your app should probably create the directory if it doesn't already exists. Commented Oct 15, 2020 at 9:22
  • Does you app has the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions? Commented Oct 15, 2020 at 9:23
  • 1
    You do not need any permission and the directory is already there. On your PC only Android Studio/Delphi can show you that location and i am not shure about Delphi. Also file manager apps on your device will not show you this directory as they have no access to your apps private folder. Commented Oct 15, 2020 at 9:34
  • You can -with code- check if a file exist. And you can read that file for further use. Do it and you will see. Commented Oct 15, 2020 at 9:35
  • 2
    GetHomePath/TPath.GetDocumentsPath is private to your app, it is not exposed to other apps or the PC. If you want to make a file accessible to them, it needs to be created in a public folder instead, like TPath.GetSharedDocumentsPath. Also, just FYI, last time I checked (albeit that was a long while ago), FireMonkey did a pretty lousy job of reporting errors for various operations, like creating files, so more times than not you wouldn't know if such operations actually succeeded or failed. Commented Oct 15, 2020 at 17:59

2 Answers 2

2

So, the solution was: Set the App-Permisisons for Storage (Settings - Apps - MyApp - Permission - Storage).

Once this was done, using the TPath.GetPublicPath to store the file resulted in a file on the Device (Internal Storage) on: /storage/emulated/0/Android/data//files Where '/storage/emulated/0' = the rootfolder of the internal storage of my device.

Sign up to request clarification or add additional context in comments.

Comments

0

If you have concern if the internal root path changes between devices you can try this

AppPath := ReplaceStr(System.IOUtils.TPath.GetSharedDownloadsPath,'/Download','');

will give you what you want so you don't have to worry if it is '/storage/emulated/0' or something else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.