Home > Enterprise >  Cannot read a file from Downloads folder on Android
Cannot read a file from Downloads folder on Android

Time:08-02

I have text files with a custom extension. I have deployed one with my Delphi FMX application to the Documents folder, and this one loads fine using TStringList.LoadFromFile(Path). The path is:

/data/user/0/com.embarcadero.[appname]/files/[filename]

I then try to load a file from the following path:

/storage/emulated/0/Download/[filename]

FileExists(Path) returns true, indicating that the file does exist. However, it fails in TStringList.LoadFromFile(Path).

I assume that this is some permissions issue. However, I have checked that 'Read external storage' is enabled in the Delphi project options under 'Uses Permissions', so the manifest should have that included. Are there any other permission settings required for this location?

Note that I'm testing on a Huawei P10 with Android 8.

CodePudding user response:

The files load after using PermissionService.RequestPermissions for ReadExternalStorage, as per the Object Pascal/Mobile Snippets/CameraRoll sample.

CodePudding user response:

  {$IF DEFINED (ANDROID)}
  var FStorage := '/storage/emulated/0/RAH Download';
  {$ELSEIF DEFINED (MSWINDOWS)}
  var FStorage := ExpandFileName(GetCurrentDir)   PathDelim   'Download';
  {$ELSE}
  ShowMessage('Not Support';
  Exit;
  {$ENDIF}

  if not DirectoryExists(FStorage) then begin
    if not CreateDir(FStorage) then
      Exit;
  end;

===========================================================

  var HTTP := TNetHTTPClient.Create(nil);
  try
    var Stream := TMemoryStream.Create;
    try
      HTTP.Get(FURL, Stream);
      Stream.SaveToFile(FStorage   Pathdelime   'FileName');
    finally
      Stream.DisposeOf;
    end;
  finally
    HTTP.DisposeOf;
  end;
  • Related