I have problem to find and open a file that I have stored on my phone from my pc. Despite the fine solution in this answer I can't get it to work. I'm running on a HTC Sensation Z710e
Here is the code I'm trying to run:
function GetSDCardPath: string;
var MusicPathLength: integer;
MusicPath, SDCardPath: string;
begin
MusicPath:=System.IOUtils.TPath.GetSharedMusicPath;
MusicPathLength:=Length(MusicPath);
SDCardPath:=Copy(MusicPath, 0, MusicPathLength-5);
Result:=SDCardPath;
end;
procedure TForm3.Button1Click(Sender: TObject);
var sr:TSearchRec;
begin
CardPath:=TPath.Combine(GetSDCardPath,'*.*');
if (FindFirst(CardPath,faNormal,sr)=0) then
begin
repeat
Memo1.Lines.Add(sr.Name);
until FindNext(sr)<>0;
FindClose(sr);
end;
end;
I did a second test with the code below and I can obviously store a file as the filename comes up in the filelist, but it seems like it not is stored on the sdcard, at least not the one that appears as my external drive F: on my pc. The TPath.GetDocumentsPath should point to the sdcard, shouldn't it?
procedure TForm3.Button1Click(Sender: TObject);
var sr:TSearchRec;
begin
CardPath:=TPath.Combine(TPath.GetDocumentsPath,'*.*');
Memo1.Lines.Add(CardPath);
if (FindFirst(CardPath,faAnyFile,sr)=0) then
begin
repeat
Memo1.Lines.Add(sr.Name);
until FindNext(sr)<>0;
FindClose(sr);
end;
end;
procedure TForm3.WriteClick(Sender: TObject);
var
s: string;
F:TextFile;
begin
Memo1.Lines.Clear;
s := TPath.Combine(TPath.GetDocumentsPath,'file2.txt');
AssignFile(F,s);
ReWrite(F);
Writeln(F,'Test');
CloseFile(F);
end;
First I click the Write button to write the file and then I list the files in the directory by clicking the Button1. The CardPath is /data/data/com.embarcadero.TestApp2/files/. I have a Android/data/com.embarcadero.TestApp2/files/ folder visible on my device from my pc, but no file there. Is the file stored internaly in my device?
I finally found out how to solve this. By using TPath.GetSharedDocumentsPath I could see the file saved from the app on the pc if the device NOT was connected as a drive when saving (mentioned on this page). E.i. When using the app, the pc can't be using the sdcard as a drive at the same time.