usilo
usilo

Reputation: 365

android: Skip ACTION_CREATE_DOCUMENT if it exists

I would like to ask the user to create a folder at the root of his volume if it doesn't exist yet. This has to be achieved through SAF and with ACTION_CREATE_DOCUMENT to get the rights to read/write on the sdcard.

I used to check if the directory exists by

  1. asking access to the document tree with "ACTION_OPEN_DOCUMENT_TREE"
  2. check with if (!DocumentFile.fromTreeUri(this, workingDirUri).exists())

But with Android 11 it is not possible anymore to do the ACTION_OPEN_DOCUMENT_TREE on the root of the volume, and ACTION_OPEN_DOCUMENT doesn't permit to select a directory). If I try to bypass the ACTION_OPEN_DOCUMENT_TREE and check if the directory exists with the method above, android 11 returns a "Permission denied, you need to grant permissions to with ACTION_OPEN_DOCUMENT" or something like that, I don't remember the exact wording.

For now, my workaround is to use the File class to check if the directory exists, but this is not very elegant.

How would you do this by staying with SAF calls? Is there an option to tell ACTION_CREATE_DOCUMENT to skip if the document already exists?

BTW: there is a parent unanswered question here asking for overriding, which is not exactly my issue.

Thanks

Upvotes: 0

Views: 439

Answers (1)

blackapps
blackapps

Reputation: 9292

On Android 11:

If you use ACTION_OPEN_DOCUMENT_TREE you can let the user choose 'WorkingDir' (if it exists already) or let the user create and select it.

You are done then as the uri you obtain exists.

Upvotes: 0

Related Questions