Search code examples
javaandroiduriandroid-permissionsstorage-access-framework

Storage Access Framework - Saving Uri


My app needs to archive a folder Uri (from an Inten with ACTION_OPEN_DOCUMENT_TREE and the right flags inside that has been returned to the app from user selection) in the suitable form to use it next time the app is run or the device is booted (takePersistableUriPermission is used).

Saving the Uri path as a string seems not to be enough, because

Uri uri;
uri=Uri.parse(uriPath);
docUri= DocumentsContract.buildDocumentUriUsingTree(uri,
            DocumentsContract.getTreeDocumentId(uri));

gives an error like invalid Uri.

Saving also the Uri id is not useful.

I see that the Uri has many parameters inside. I checked which parameters have values inside my sample Uri and a few are.

So I would know how to save a Uri so the app has all necessary data to recreate it and the DocumentsContract class (or similar) can query its parameters not throwing any exceptions.

Any suggestion is welcome


Solution

  • The right way to archive a Uri is by means of saving the uri.toString() value.

    Then to retrieve it: uri=Uri.parse(archivedUriString);