我正在使用 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;
TPath.Combine()
而不是Format()
。你确定 SDCard 路径以/data/data/
开头而不仅仅是/data
吗?你是如何检索 HomePath 的?此外,为什么要使用单独的TStringList
而不是使用Memo.Lines.Load...()
和Memo.Lines.Save...()
? - Remy LebeauAppPath:= TPath.GetHomePath; FileName:= TPath.Combine(AppPath,'File.txt');
- xJernej