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