0

Is SAF intended to replace File handling in external directories, in a certain future?

I hope it is not so in the end, but I am testing SAF itself in advance. It would be useful for other reasons too.

Let's say I am using this method:

static public DocumentFile createFolderInFolder(Activity activity,String            parentFolderUriString,String folderName)
{
DocumentFile result=null;
ContentResolver contentResolver;
contentResolver = activity.getContentResolver();
Uri parentFolderUri=null;
Uri oldParentUri = Uri.parse(parentFolderUriString);
String id = DocumentsContract.getTreeDocumentId(oldParentUri);
parentFolderUri= DocumentsContract.buildChildDocumentsUriUsingTree(oldParentUri,id);
DocumentFile parentFolder = DocumentFile.fromTreeUri(activity, parentFolderUri);
result=parentFolder.createDirectory(folderName);
return result;
}

It creates a directory inside another directory.

When attempting to create an existing folder, for instance "folderName", a new folder is created with a similar name "folderName (1)".

Is there a way to avoid this, so that a new folder is not created if existing? (And the same for files)

P5music
  • 3,197
  • 2
  • 32
  • 81

1 Answers1

1

parentFolderUri= DocumentsContract .buildChildDocumentsUriUsingTree(oldParentUri,id);

For creation in the right directory that should be .buildChildDocumentsUriUsingTree(oldParentUri,id + "/" + folderName);

Further you should start programming two functions. One to check if a directory exists and one for file existence.

To check if a directory exists you would query() the content provider for the (directory+dirName) uri. If you get exceptions the directory does not exist.

For file existence try to use the OpenInputStream of the content resolver on the file uri. If it opens the file exists. If you get exceptions the file does not exist.

Update: If you want to check if a folder exists in a path you obtained with ACTION_OPEN_DOCUMENT_TREE you can use:

static public boolean folderExists(Activity activity, String rootPath, String folderName)
{
    return DocumentFile.fromTreeUri(activity, Uri.parse(rootPath)).findFile(folderName)!=null;

}
blackapps
  • 8,011
  • 2
  • 11
  • 25
  • My code works, it creates the subfolder in the right folder. But If I attempt the same creation of the same subfolder in the same folder, it creates another subfolder with a suffix like (1). It is not like in the normal filesystem. Your answer is maybe for this question: https://stackoverflow.com/questions/58006838/android-saf-method-checking-folder-existence-always-yields-true – P5music Sep 26 '19 at 08:35
  • Yes i know that it creates another folder with an (1). Thats why you should check if the folder already exists before you try to create it. – blackapps Sep 26 '19 at 09:28
  • Please can you look at https://stackoverflow.com/questions/58006838/android-saf-method-checking-folder-existence-always-yields-true – P5music Sep 26 '19 at 11:41
  • Repeat: To check if a directory exists you would query() the content provider for the (directory+dirName) uri. If you get exceptions the directory does not exist. – blackapps Sep 26 '19 at 13:06
  • Why not answering the other question (with some code)? So I could accept your answer and you earn some points. – P5music Sep 27 '19 at 08:09
  • First i expect you to to implement the suggested query() yourself. If you have specific problems i'm willing to help. Start with googling getContentResolver().query() ACTION_OPEN_DOCUMENT_TREE. You will find https://stackoverflow.com/questions/44185477 and more. It gives you a start. – blackapps Sep 27 '19 at 14:14
  • I ended up using a simpler way, without querying the ContentProvider, I answered that question myself. Please give it a look if you are interested. – P5music Sep 27 '19 at 14:18
  • Nice you found the solution yourself. Is the folderUriString the same as in the question? How did you obtain/construct it? – blackapps Sep 27 '19 at 14:26
  • You were right about the parent folder having to be the starting point. I used the findFile() method on it. – P5music Sep 27 '19 at 14:43