Select and load txt file to android application (Delphi-XE5)

北城以北 提交于 2020-01-06 13:59:10

问题


I would like to browse SD card, select dir and file and load txt file to my Android application created by Delphi XE5.

Is any standart component or methods for this ? Like OpedFileDialog ?


回答1:


There is no equivalent for TOpenFileDialog on Android. It's not part of the operating system, and is not available from the Component Palette when targeting Android.

You can see this by viewing the form in the designer, and then checking the Dialogs tab in the Component Palette; all of the components are disabled, meaning they're not available for the targeted platform. Hovering the mouse over any of them indicates they're available for Win32, Win64, and OS X, but not iOS or Android.

You could always build your own, based on a TForm (or better yet, a TPopup, which is a better fit for the typical application flow for a mobile device), using the functionality available in IOUtils.TPath for retrieving directories and filenames. Once you have the filename, the functionality of loading it is simple and available in several ways - here are a few:

  • Load it using TFile.ReadAllLines (again from IOUtils)
  • TStringList.LoadFromFile
  • Using TFileStream.LoadFromFile
  • Using a TMemo.Lines.LoadFromFile



回答2:


Use TStringList, with the 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;


来源:https://stackoverflow.com/questions/19832244/select-and-load-txt-file-to-android-application-delphi-xe5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!