Search code examples
androidandroid-contentproviderstorage-access-framework

Persistent permissions to non-SAF content


In my app, I'd like to store a persistent read permission to content provided by Dropbox (among other content providers). The Android Dropbox app doesn't support the Storage Access Framework, so to be able to select content I can't use ACTION_OPEN_DOCUMENT -- instead I need to use ACTION_GET_CONTENT.

However, it seems that some content providers, such as Drive, don't return persistable permissions for URIs returned via ACTION_GET_CONTENT. I believe this is as expected, because GET_CONTENT URIs are not supposed to be persistable. Unfortunately I do need to persist the reference across restarts.

It seems that there is no way to get persistent permissions to a URI in recent API versions if the content provider doesn't support SAF. Is that true? What is a good workaround?

Bad (for my use case) workarounds would be: copying the content and storing it locally, relying on implementation details which are not in spec (e.g. it seems that Dropbox URIs returned by GET_CONTENT are in fact persistable), or not persisting the permission.


Solution

  • It seems that there is no way to get persistent permissions to a URI in recent API versions if the content provider doesn't support SAF. Is that true?

    Based on my experiments, yes. More accurately, AFAICT, only Uri values obtained from a DocumentsProvider have a shot at having persistable permissions, in terms of what the framework offers. I do not see how an ordinary ContentProvider can offer this.

    What is a good workaround?

    Given your list of "bad" workarounds, your best workaround is to use some Dropbox-specific API to allow the user to choose the content and for you to access it over time, if Dropbox offers one.

    Of the "bad" workarounds, copying the content is a likely choice — adjust your UI to tell the user that you are "importing" the content, for example, to help indicate that it is indeed a copy.