问题
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 fromIOUtils
) 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