How to check if parent folder of a child file's DocumentFile has permission granted/revoked?

236 views Asked by At

I have asked permission for a folder (Intent.ACTION_OPEN_DOCUMENT_TREE) and created a file using folderDocumentFile.createFile() inside the folder.

I have saved the fileDocumentFile's Uri for future read actions.

Accessing the file using its DocumentFile.fromSingleUri() works fine. But when testing whether I have permission to access the file, I need to check the permission granted for the parent folder.

Based on the file's Uri, how can I get the Uri for the root of the tree for which permissions have been granted?

I've tried:

  • fileDocumentFile.parentFile?.uri but that return the same Uri as for fileDocumentFile.
  • fileDocumentFile.parentFile?.parentFile?.uri returns null.
  • And various other suggestions found

I could perhaps use regex to alter the parent's uri string, but would rather prevent that. I also would like to prevent to also save the Uri of the tree.

1

There are 1 answers

2
blackapps On

If you want to check if the uri for a file you created on a folder obtained with ACTION_OPEN_DOCUMENT_TREE is still valid, the simplest thing to do is trying to open an inputstream for it using the content resolver.

You will get a SecurityException if meanwhile the permanent permission for the foldere is released or revoked.

Trying to create a DocumentFile instance for the file uri will not fail. So this is what i suggested to do earlier and is useless.