选择txt文件并将其加载到Android应用程序(Delphi-XE5)



我想浏览SD卡,选择dir和文件,然后将txt文件加载到Delphi XE5创建的Android应用程序中。

是否有任何标准组件或方法?喜欢 OpedFileDialog ?

在 Android 上没有等效的 TOpenFileDialog。它不是操作系统的一部分,在面向 Android 时,无法从组件面板中使用。

您可以通过在设计器中查看窗体,然后检查组件面板中的"Dialogs"选项卡来查看此内容;所有组件都已禁用,这意味着它们不可用于目标平台。将鼠标悬停在它们中的任何一个上表示它们可用于 Win32、Win64 和 OS X,但不适用于 iOS 或 Android。

您始终可以基于 TForm(或者更好的是,TPopup 构建自己的,它更适合移动设备的典型应用程序流),使用 IOUtils.TPath 中提供的功能来检索目录和文件名。获得文件名后,加载它的功能很简单,可以通过多种方式使用 - 这里有一些:

  • 使用 TFile.ReadAllLines 加载它(再次从 IOUtils
  • TStringList.LoadFromFile
  • 使用TFileStream.LoadFromFile
  • 使用TMemo.Lines.LoadFromFile

使用 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;

最新更新