URI returned by ActivityResultContracts.OpenDocument
can be "opaque" (I don't know a better term) in a sense that you can't write to the associated file using it (openOutputStream
throws IllegalArgumentException
with message "Media is read-only"), you can't determine its parent directory etc. because it is in the form of e.g. "/document/image:821286". I can read the file data using it though, and not all URIs returned by OpenDocument
launcher are "opaque".
I know that I do have all the access I need to all those files, because if the same file is returned by ActivityResultContracts.CreateDocument
the returned URI is never "opaque", so you can write to it, get the containing directory etc. In that case the URI is something like "/document/primary:DCIM/Camera/IMG_20211007_101132.jpg".
I do have WRITE_EXTERNAL_STORAGE
permission granted and this is being tested on Android 9.
I am looking for a way to either always get "transperent" URIs from OpenDocument
launcher or to convert "opaque" to "transparent" URI; please write below how to accomplish either of those.
CodePudding user response:
in a sense that you can't write to the associated file using it (openOutputStream throws IllegalArgumentException with message "Media is read-only")
That is an unfortunate limitation of the Storage Access Framework: poorly-written document providers can elect to not allow read-write access to the content, and we have no means of specifying that we only want writeable documents. We have to gracefully fail in those cases.
because if the same file is returned by ActivityResultContracts.CreateDocument the returned URI is never "opaque"
That is not true. You tested with one document provider out of many, and that document provider unfortunately leaks information. There is no requirement for any given device to have a document provider that behaves that way, and there is no requirement that the user use such a document provider.
Document providers do not have to work with publicly-accessible filesystem paths, let alone tell you what those paths are. Document providers are welcome to store documents on network servers, cloud storage providers (e.g., Google Drive), encrypted data stores, etc.
I am looking for a way to either always get "transperent" URIs from OpenDocument launcher or to convert "opaque" to "transparent" URI; please write below how to accomplish either of those.
That is not possible.