0

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.

passerby
  • 1,807
  • 19
  • 26
  • `But when testing whether I have permission to access the file, I need to check the permission granted for the parent folder.` No. Just check the permission on the file. – blackapps Dec 19 '22 at 12:02
  • If fromSingleUri() gives you a DocumentFile instance then you are done. What makes you think you need to check something? – blackapps Dec 19 '22 at 12:05
  • I wonder: did you take persistable uri permission on the tree? (Not that it matters i think.. but not shure). – blackapps Dec 19 '22 at 12:07
  • `Based on the file's Uri, how can I get the Uri for the root of the tree ` Well if you look at some content schemes of files in a folder amd the content scheme of the folder then an algorithm to do so will come to mind. – blackapps Dec 19 '22 at 12:10
  • `. I also would like to prevent to also save the Uri of the tree.` If you took permanent uri permission Android OS remembers them for you. You can always request Android OS to give you the list of all permanent uri permissions. – blackapps Dec 19 '22 at 12:17
  • @blackapps, That only works when permission has been asked for a file using `Intent.ACTION_OPEN_DOCUMENT`. I asked permission for a tree. – passerby Dec 19 '22 at 13:04
  • @blackapps, Yes, the OS will remember the permissions granted, but permissions can be revoked by the user using Settings. Therefor I would like to test if permission still exists. – passerby Dec 19 '22 at 13:08
  • If the permission still exists the uri will be in the list you requested from OS. If it is revoked by the user it does not exist and will not be in the list. – blackapps Dec 19 '22 at 13:42
  • @blackapps, Can't beat that logic... But when asking permission for a tree, only the permission for the tree will be in the list. When creating a file inside the tree, the uri of the file will not be in the list of permissions. I only saved the uri of the file... – passerby Dec 19 '22 at 14:02

1 Answers1

0

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.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • I've done something similar using `DocumentFile.exists(Uri)`. Android will throw a SecurityException when no permission is being granted. I'm looking for a more elegant solution though. I would expect it to be possible to get the parent's uri. I can also make it work using Regex, but that's not so nice too... – passerby Dec 19 '22 at 15:10
  • Note, when using `MethodChannel` in Flutter to access native Android, Exceptions are not passed back. The channel can only return an exception using `Result.error(code, message, details)` which will be caught in Flutter as a generic `PlatformException`. – passerby Dec 19 '22 at 15:10