4

I am using Delphi XE5 for Android developing.

I like to save and load TStringList to text file on SDCard. When I save TStringList to file all is OK. After saving I can call LoadFromFile and file is loaded.

The problem is when I close the application and open it again. File does not exist? This is file location FileName := '/data/data/[com.MY.APP]/files/File.txt'

Under application user permissions = Write external stoage : True

Do I need to save the file to another folder?

Thx for help.

This in my code and settings.

procedure LOAD;
var
  TextFile: TStringList;
  FileName: string;
begin
     TextFile := TStringList.Create;
    try
      FileName := Format('%s/File.txt', [GetHomePath]);
      if FileExists(FileName) then
      begin
        TextFile.LoadFromFile(FileName);
        Memo1.Lines.Text := TextFile.Text
      end
      else
        ShowMessage('File not exists!');
    finally
      TextFile.Free;
    end;     

end;

procedure SAVE;
var
  TextFile: TStringList;
  FileName: string;
begin   
    TextFile := TStringList.Create;
    try
      FileName := Format('%s/File.txt', [GetHomePath]);
      TextFile.Text := Memo1.Lines.Text;
      TextFile.SaveToFile(FileName);
    finally
      TextFile.Free;
    end;     
end;

enter image description here

enter image description here

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
xJernej
  • 194
  • 2
  • 3
  • 14
  • You should use `TPath.Combine()` instead of `Format()`. Are you sure the SDCard path starts with `/data/data/` and not just `/data`? How are you retriving the HomePath? Also, why use a separate `TStringList` instead of using `Memo.Lines.Load...()` and `Memo.Lines.Save...()`? – Remy Lebeau Dec 14 '13 at 18:56
  • GetHomePath is system function from System.SysUtils Separate TStringList is just for demo. If I Use just /data/ I get exception 'Cannot create file "/data/com.debersek.KKP/files/File.txt". Not a directory'. – xJernej Dec 14 '13 at 19:27
  • 1
    You're not saving anywhere near the SD card. See my answer [here](http://stackoverflow.com/a/18857868/62576). – Ken White Dec 15 '13 at 20:38
  • Thx for help. This is working for me: `AppPath := TPath.GetHomePath; FileName := TPath.Combine(AppPath, 'File.txt');` – xJernej Dec 19 '13 at 19:53

1 Answers1

3

yes,

AppPath := TPath.GetHomePath; 
FileName := TPath.Combine(AppPath, 'File.txt');

is working fine. do not forget to add the unit System.IOUtils to your uses clause.

thank you xJernej.

3XTON
  • 41
  • 3