0

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?

Community
  • 1
  • 1
Lars Ljungberg
  • 313
  • 1
  • 6
  • 19
  • I forgot to mention that the directory structure seen from the pc and what I read from the different TPath.GetPath strings differ so much that I can't figure out where my file is located. – Lars Ljungberg Jun 23 '14 at 09:59
  • What is the exact path according to your pc? You call a function `GetSDCardPath` but there you use only `GetSharedMusicPath` which probably is not on your removable sd card. What is the exact path you see on your device? You forgot to tell that. Is the file on the removable sd card according to your pc? – greenapps Jun 23 '14 at 11:47
  • Added another testcode. – Lars Ljungberg Jun 23 '14 at 13:40
  • /data/data/..... is internal memory. Android/data/data/.... is external memory which can be in the device or on the removable sd card. Internal memory can not be seen by your pc/Eclipse. – greenapps Jun 23 '14 at 13:54
  • I'm not using Eclipse. I'm just browsing my device as an external drive. What path to use so both my pc and my app can access the same folder ? – Lars Ljungberg Jun 24 '14 at 05:34
  • Well that is clear now. Not internal memory paths but external memory paths. Those are all the paths you showed (in your link) wich contain /Android/. That is the Android you see on your pc. – greenapps Jun 24 '14 at 06:27
  • Install a File Explorer app on your device to find out more about the file structure and paths. Doesn't Delphi provide a functions getExternalFilesDir() and getStorageDirectory() and so on? – greenapps Jun 24 '14 at 06:31
  • Got it work. See how in my answer below. – Lars Ljungberg Jun 26 '14 at 13:30

1 Answers1

1

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.

Lars Ljungberg
  • 313
  • 1
  • 6
  • 19