1

This answer has some good information on using ACTION_OPEN_DOCUMENT_TREE:

How to use the new SD card access API presented for Android 5.0 (Lollipop)?

In this answer is the statement "Yep, the ACTION_OPEN_DOCUMENT_TREE intent gives you recursive access to both existing and newly created files and directories."

I'm not entirely certain I'm interpreting this correctly because this is not what I am finding in my experience. When the user selects a root directory, I am persisting the permission for:

content://com.android.externalstorage.documents/tree/primary%3A

Later on I want to access a child path at:

content://com.android.externalstorage.documents/tree/primary%3APictures%2FScreenshots

I then try to access the DocumentFile as follows:

DocumentFile fromDir = DocumentFile.fromTreeUri(this, fromUri);
boolean canRead = fromDir.canRead();

I am unable to read the fromDir and I can only read from the root directory that I have granted permission for.

Is there anything that I am missing that I need to do to request recursive permission or any other easy way to achieve this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
JamieH
  • 4,788
  • 6
  • 28
  • 29
  • 1
    "Later on I want to access a child path at" -- it would appear that you manually assembled this `Uri`, based on how you are trying to use it (`fromTreeUri()`). Try using `listFiles()` on `DocumentFile` to drill down to what you are seeking. – CommonsWare Jun 15 '16 at 10:55
  • I'm the developer of an automation app called MacroDroid and one feature it supports is automated file copying. The child path was chosen earlier by the user via the same mechanism and I am persisting the URL in my own data. I'm trying to come up with an easy way to re-instate the permissions in the case where the user re-imports his data (e.g. on reinstall etc.). I figure just setting the root once as a permission would be the simplest. Thanks for the suggestion, I'll have a play with the listFiles() and see if I can marry the two things up in some way. – JamieH Jun 15 '16 at 11:36

0 Answers0