Let me explain...
WHAT THE APP DOES
- An app allows the user to select an XML file from phone storage.
- The XML is opened through an
ACTION_GET_CONTENT(changed that toACTION_OPEN_DOCUMENT) Intent. With the XML Uri, a temporaryFile(the XML) is created in order for the app to read its contents (the app was prepared to receive and process Files not Uris. - Among the XML data, there's an audio file path, which is always located where the XML was, such:
- XML path: myStorage/myProject/example.xml
- Audio file: myStorage/myProject/audio/audioSample.mp3
- After reading all the XML data, the user opens a new fragment where there's a MediaPlayer that has to play the "audioSample.mp3" file.
- The Media player was using some *app internal assets path as audio test files, like
"content://" + "com.my.app/" + "audio/audioSample.mp3". The MediaPlayer also has a method that, instead of a "path string (as shown)", can use a "Uri" to play media.
THE PROBLEM
- I've been unable to play the media when loaded from outside internal assets
WHAT I TRIED
- Using the Uri and Path string options of the MediaPlayer
- Hardcoding the Absolute Path string of the "audioSample"
- Creating a new Uri from the XML original Uri, such as "content://interalStorage/myProject:audio/audioSample.mp3"
- Encoding the last part of that Uri path (as I checked what system's Uri path looks like if the user selects the audioFile instead of the XML file)
- Just copying that system Uri path of the audio file and hardcode it
- When I finally got the correct (or what I think is correct) Uri path, like above, I got a
No persistable permission grants found for UID 10476 and Uri content://com.android.externalstorage.documents/document/primary:myProject/audio/audioSample.mp3. Now, I looked that up and saw thetakePersistableUriPermissionoption, but:
- It does not work well with
fileChooserResultLauncherinstead of old, deprecated,onActivityResult. - It looks like it's just for one file, not the whole directory of "myProject"; so there's the option of
ACTION_OPEN_DOCUMENT_TREE.
Any other approach aside from ACTION_OPEN_DOCUMENT_TREE?
PS: Can I include that in the Intent or I must ask the user twice (one for selecting the XML the other for the Directory as a whole)?
With the XML Uri, a temporary File (the XML) is createdThat makes no sense. Even if you manage you got no read permission. Use the uri to open the file.It looks like it's just for one file, not the whole directory of "myProject"Of course. You selected a file. Select directories with ACTION_OPEN_DOCUMENT_TREE.Fileit's because the app was designed to handle aFiletype (in several other parts of the code), that's why I need to handle it like that. Any approach aside from addingACTION_OPEN_DOCUMENT_TREE? Thank you!