5

How do I list files in SD card path and internal memory path?

I've tried FindFile with GetDocumentsPath as the parameter and i have no luck.

Ouch, where can I find more documentation or code snippets for Delphi for Android?

Kromster
  • 7,181
  • 7
  • 63
  • 111
alycia
  • 59
  • 1
  • 8

1 Answers1

4

Add System.IOUtils to your uses clause. You can then use code TPath and TDirectory something like this:

uses
  System.IOUtils, System.Types;

procedure TForm1.Button1Click(Sender: TObject);
var
  FileList: TStringDynArray;
  DocDir: string;
  s: string;
begin
  Memo1.Lines.Clear;
  DocDir := TPath.GetDocumentsPath;
  FileList := TDirectory.GetFiles(DocDir);
  for s in FileList do
    Memo1.Lines.Add(s);
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Yeah, I also get the code from Marco Cantu's pdf and no file were found? – alycia Sep 18 '13 at 15:30
  • Do you have files in the device documents folder? Change the code to write `DocDir` to the memo just before the `FileList :=` line, and see if it's where you expect it to be. (I didn't know Marco had a PDF out for XE5 yet. Is it on the usual web site?) – Ken White Sep 18 '13 at 15:38
  • Sorry, I was trying in emulator. I'm not in my computer. I will try it later with a phone. IIRC, if I run the code in emulator then the path is /data/data/embarcadero.program/. Is /data in Android same with drive letter in Windows? How's about internal memory path? Do Delphi has the API? I got the PDF from http://www.embarcadero.com/resources/white-papers?download=102 – alycia Sep 18 '13 at 16:11
  • Other operating systems don't have drive letters. :-) That's why IOUtils has TPath, that lets you call one function and get the proper location regardless of platform. IOUtils.TPath has other functions for finding folders, like `GetHomePath` and `GetTempPath`. They're documented (well, at least listed) in the help and, of course, in the RTL source code. – Ken White Sep 18 '13 at 17:12
  • You might want to see [this answer](http://stackoverflow.com/a/18857868/62576) to another question as well. – Ken White Sep 18 '13 at 17:29
  • Just turned on my laptop. Yeah I've already see that question. Why can't I see RTL source code? Should I download the full ISO image? "Unable to locate file 'System.IOUtils.pas'." I only download radstudio_xe5_esd.exe with trial license. – alycia Sep 19 '13 at 00:59
  • You don't have source code, because you're using the trial. You have something else wrong, though; System.IOUtils.dcu should be in `$(DELPHI)\Lib\$(Platform)\$(Target)` , where `$(Platform)` is OSX, Android, iOS, etc. and `$(Target)` is `Release` or `Debug`, and should be found automatically by the compiler. Are you sure you've selected an appropriate target for the project? – Ken White Sep 19 '13 at 01:06
  • Sure, Target Platforms (Android) with Android option is choosen. http://s12.postimg.org/hf9d78fh9/Capture.gif. You mean the correct error message is "Unable to locate file 'Android\System.IOUtils.pas'."? – alycia Sep 19 '13 at 02:19
  • No. :-) I mean you should not get this error message at all if you've configured your project correctly. There should be no need for the .pas file, because it should find the already-compiled `System.IOUtils.dcu` in the appropriate `lib\$(Platform)\$(Target)` folder based on the project platform and configuration (release or debug) you've selected. I don't know where the dialog you show came from; if the file can't be found, it should be a compiler error. Where did you add the `System.IOUtils` requirement? It should just be `System.IOUtils` in the uses clause. – Ken White Sep 19 '13 at 02:24
  • Why TPath.GetHomePath and TPath.GetDocumentsPath showed same path? I still could not list all files in internal storage path. – alycia Sep 19 '13 at 03:52