DELPHI XE5 Android - 从SD卡保存/加载文件

4

我正在使用 Delphi XE5 开发 Android 应用。

我喜欢将 TStringList 保存和加载到 SDCard 上的文本文件中。 当我将 TStringList 保存到文件时一切正常。保存后,我可以调用 LoadFromFile 并加载文件。

问题是当我关闭应用程序并重新打开它时,文件不存在? 文件位置为 FileName := '/data/data/[com.MY.APP]/files/File.txt'

在应用程序用户权限下 = Write external stoage : True

我需要将文件保存到另一个文件夹吗?

感谢您的帮助。

这是我的代码和设置。

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


你应该使用 TPath.Combine() 而不是 Format()。你确定 SDCard 路径以 /data/data/ 开头而不仅仅是 /data 吗?你是如何检索 HomePath 的?此外,为什么要使用单独的 TStringList 而不是使用 Memo.Lines.Load...()Memo.Lines.Save...() - Remy Lebeau
GetHomePath是System.SysUtils中的系统函数。而TStringList只是用于演示。如果我只使用“/data/”,则会出现异常:“无法创建文件“/data/com.debersek.KKP/files/File.txt”。不是目录。”。 - xJernej
1
你保存的数据远远不足以存储在SD卡中。请查看我的答案这里 - Ken White
感谢您的帮助。这对我有用:AppPath:= TPath.GetHomePath; FileName:= TPath.Combine(AppPath,'File.txt'); - xJernej
1个回答

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

运行良好。不要忘记在使用从属关系中添加System.IOUtils单元。

谢谢xJernej。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接