Open file in Android app based on Uri path

140 views Asked by At

Let me explain...

WHAT THE APP DOES

  1. An app allows the user to select an XML file from phone storage.
  2. The XML is opened through an ACTION_GET_CONTENT (changed that to ACTION_OPEN_DOCUMENT) Intent. With the XML Uri, a temporary File (the XML) is created in order for the app to read its contents (the app was prepared to receive and process Files not Uris.
  3. 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
  1. 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.
  2. 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 the takePersistableUriPermission option, but:
  1. It does not work well with fileChooserResultLauncher instead of old, deprecated, onActivityResult.
  2. 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)?

0

There are 0 answers