How and where to create files on Android with Delphi

Asked

Viewed 3,507 times

2

In my application in Delphi I am creating a simple 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 Debug that the file is created. When I run for the second time the if not TFile.Exists also detects that the file already exists.

PROBLEMS:

1) When I try to find the file test.txt on Android, even I find this directory "/data/data/...";

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

  • The problem is that new versions of Android does not allow you to access some folders

1 answer

0

    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 Angelim based on the reply of this link

  • Thank you Lucas, but the problem is not creating the file but 1) Find the file I created and, 2) Create the file in a specific folder, if feasible, such as the application installation folder. You know about this information?

  • Sorry if it seemed a little confusing, but while saving the above file, it uses the Gethomepath function when the environment is Android, maybe to fetch the created file, the same function is useful. Anyway, I could not simulate here (environment did not help). Arriving at home, put here.

  • Hi Lucas. I think my doubts were not clear: At first, imagine that you created a TXT file with your application... now imagine that you go "at hand" on your Android file system and directories and want to find that file that your application created, OK? I am not finding, despite knowing that such TXT file was created successfully by my application. On the second question, I want to choose a specific folder to save the file, such as the application installation folder. But I don’t know if I can do it and I don’t know if it’s suitable/viable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.