Provide file paths to shared storage?

In one of my extensions I currently have this method to handle file paths: ASD/Private Dir/Assets...

private String resolveFileName(String fileName) {
    String completeFileName = fileName;
    if (fileName.startsWith("/")) {
      completeFileName = getExternalStoragePath() + fileName;
    } else if (fileName.startsWith("//")) {
      if (isRepl) {
        fileName = fileName.substring(2);
        completeFileName = getReplFilePath() + fileName;
      }
    } else {
      completeFileName = GetPrivateDirPath() + "/" + fileName;
    }
    return completeFileName;
  }

but it doesn't provide for paths supplied by the user to shared storage directories (I only want to offer Documents or Downloads)

I have edited the method to this:

private String resolveFileName(String fileName) {
    String completeFileName = fileName;

    if (fileName.startsWith("/") && !fileName.startsWith("/storage/emulated/0/Documents") && !fileName.startsWith("/storage/emulated/0/Download"))
    {
      completeFileName = getExternalStoragePath() + fileName;
    }
    else if (fileName.startsWith("/storage/emulated/0/Documents") || fileName.startsWith("/storage/emulated/0/Download"))
    {
      completeFileName = fileName;
    }
    else if (fileName.startsWith("//"))
    {
      if (isRepl) {
        fileName = fileName.substring(2);
        completeFileName = getReplFilePath() + fileName;
      }
    } else
    {
      completeFileName = GetPrivateDirPath() + "/" + fileName;
    }

    return completeFileName;
  }

Is there a better, preferred method for this ? Users would be encouraged to provide the absolute path, e.g.

/storage/emulated/0/Download/myfile.txt

1 Like

Note:
/Downloads is not a Shared folder, but /Download.

1 Like

Went with my changes, works OK in the extension.

Thanks @Anke for the Downloads/Download correction, too much time using my computer's file manager.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.