Delphi XE5 Android问题定位文件

时间:2014-06-23 09:55:59

标签: android delphi delphi-xe5 file-location

我有问题要找到并打开我手机上存储的文件。 尽管this answer中有一个很好的解决方案,但我无法让它发挥作用。 我正在使用HTC Sensation Z710e

以下是我正在尝试运行的代码:

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;

我使用下面的代码进行了第二次测试,显然我可以存储一个文件,因为文件名出现在文件列表中,但好像它没有存储在SD卡上,至少不是存储在我的外部的那个开车F:在我的电脑上。 TPath.GetDocumentsPath应该指向sdcard,不应该吗?

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;

首先,我单击Write按钮编写文件,然后单击Button1列出目录中的文件。 CardPath是/data/data/com.embarcadero.TestApp2/files/ 我的设备上可以看到Android / data / com.embarcadero.TestApp2 / files /文件夹,但是没有文件。文件是否存储在我的设备中?

1 个答案:

答案 0 :(得分:1)

我终于找到了解决这个问题的方法。 通过使用TPath.GetSharedDocumentsPath,如果设备NOT在保存时作为驱动器连接,我可以在PC上看到从应用程序保存的文件(在this page上提到)。 E.I.使用应用程序时,电脑不能同时使用SD卡作为驱动器。