【发布时间】:2013-11-07 09:30:07
【问题描述】:
我想浏览 SD 卡,选择目录和文件,然后将 txt 文件加载到我由 Delphi XE5 创建的 Android 应用程序中。
有任何标准的组件或方法吗?喜欢 OpedFileDialog 吗?
【问题讨论】:
标签: delphi delphi-xe5
我想浏览 SD 卡,选择目录和文件,然后将 txt 文件加载到我由 Delphi XE5 创建的 Android 应用程序中。
有任何标准的组件或方法吗?喜欢 OpedFileDialog 吗?
【问题讨论】:
标签: delphi delphi-xe5
Android 上没有 TOpenFileDialog 的等效项。它不是操作系统的一部分,并且在面向 Android 时无法从 Component Palette 中获得。
您可以通过在设计器中查看表单,然后检查组件面板中的Dialogs 选项卡来看到这一点;所有组件都被禁用,这意味着它们不适用于目标平台。将鼠标悬停在其中任何一个上表示它们适用于 Win32、Win64 和 OS X,但不适用于 iOS 或 Android。
您始终可以使用IOUtils.TPath 中提供的功能,基于TForm(或者更好的是TPopup,它更适合移动设备的典型应用程序流程)构建自己的用于检索目录和文件名。获得文件名后,加载它的功能就很简单了,并且可以通过多种方式使用 - 这里有一些:
TFile.ReadAllLines(同样来自IOUtils)加载它TStringList.LoadFromFileTFileStream.LoadFromFile
TMemo.Lines.LoadFromFile
【讨论】:
TOpenDialog/TOpenFileDialog 来使用内部自定义对话框/表单。
使用TStringList 和TStringList.loadFromFile(file);
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;
【讨论】:
FileName 可以加载机智TStringList.LoadFromFile