How and where to create files on Android with Delphi

In my app on Delphi I am creating a plain text file on Android, using the following code:

var lst: TStringList;
begin
   lst := TStringList.Create;
   lst.Clear;
   lst.Add('a');
   lst.Add('b');
   lst.Add('c');
   lst.Add('d');

   // Esse path é: /data/data/com.embarcadero.MyApp/files/test.txt
   if not TFile.Exists(System.IOUtils.TPath.Combine(System.IOUtils.tpath.getdocumentspath,'test.txt')) then      
       lst.SaveToFile(System.IOUtils.TPath.Combine(System.IOUtils.tpath.getdocumentspath,'test.txt'));

And I see through debugging that the file is created. When I run the routine for the second time if not TFile.Exists also detects that the file already exists.

Problems:

1) when I try to find the file test.txt on Android, I don't even find this directory "/date/data/...";

2) do not know is possible on Android, but would like to save this file in the same program installation folder. In projects with the Delphi VCL I used as a reference the path of the executable Application.ExeName, which in Android is not compatible. Is it possible / indicated to do this on Android?

Author: wBB, 2017-11-21

1 answers

    procedure TForm1.Button1Click(Sender: TObject);
    var
      TextFile: TStringList;
      FileName: string;
    begin

      try
        TextFile := TStringList.Create;
        try
    {$IFDEF ANDROID}// if the operative system is Android
          FileName := Format('%smyFile.txt', [GetHomePath]);
    {$ENDIF ANDROID}
    {$IFDEF WIN32}
          FileName := Format('%smyFile.txt', [ExtractFilePath(ParamStr(0))]);
    {$ENDIF WIN32}
          if FileExists(FileName) then
          begin
            TextFile.LoadFromFile(FileName); // load the file in TStringList
            ShowMessage(TextFile.Text); // there is the text
          end
          else
          begin
            ShowMessage('File not exists, Create New File');

            TextFile.Text := 'There is a new File (Here the contents)';
            TextFile.SaveToFile(FileName); // create a new file from a TStringList

          end;
        finally
          TextFile.Free;
        end;
      except
        on E: Exception do
          ShowMessage('ClassError: ' + E.ClassName + #13#13 + 'Message: ' +
              E.Message);
      end;
    end;

Credits to the Angel, based on the answer of this link

 0
Author: Lucas de Souza Cruz, 2017-11-21 19:32:45