androidaudiofile-browser

How to let user interact with multiple audio files generated by my app


I have an Android app that targets SDK >= 33. It has the following flow currently:

  1. User inputs an audio file using registerForActivityResult(ActivityResultContracts.GetContent() with the URI audio/*
  2. My app copies this file to the app's getExternalCacheDir so my NDK function can locate it
  3. Invoke my NDK function, which reads the copied file from app-local storage, does some processing, and writes the output to the app's getExternalFilesDir(Environment.DIRECTORY_MUSIC) path

At this point, I have the list of output files that I'm ready to return back to the user as the results of processing:

com.my.app  Out file: /storage/emulated/0/Android/data/com.my.app/files/Music/processed_file_0.wav
com.my.app  Out file: /storage/emulated/0/Android/data/com.my.app/files/Music/processed_file_1.wav

What I really want is to launch the Android built-in file browser to the parent directory /storage/emulated/0/Android/data/com.my.app/files/Music/, with a button like: <Browse processed outputs>

My hope is there is a natural/built-in file browser can easily let the user do whatever they want, like share, save, copy, or open/listen/play the file - none of this is functionality that I want to re-implement in my app given a choice.

However, I think my understanding is flawed and we are not allowed to do such a thing with the Intent system. What's a good way to let a user do whatever they want with the audio files that my app has output, or to point them to browse said files?

I tried to save to the Media Store, which works - I can see the files in Audio/unknown/App Name/. Again, however, I could not find a way to launch a file browser with EXTRA_INITIAL_URI to point to this directory.

What should I do?


Solution

  • with the URI audio/*

    FWIW, audio/* is a MIME type, not a URI.

    My app copies this file

    FWIW, there is no requirement for the Uri that you get from ActivityResultContracts.GetContent to represent a file.

    What I really want is to launch the Android built-in file browser to the parent directory /storage/emulated/0/Android/data/com.my.app/files/Music/, with a button like:

    Sorry, that is not an option. There is no "built-in file browser" that is identical and guaranteed to exist across all ~3 billion Android devices. And, on modern versions of Android, any such file browser will not have access to the location that you chose.

    What's a good way to let a user do whatever they want with the audio files that my app has output, or to point them to browse said files?

    I would start by letting the user control where those documents are created.

    Use files like you are creating as in interim step (since you're using the NDK). Then let the user decide where they go in the end via ActivityResultContracts.OpenDocumentTree, copying the files to documents in that tree. Now that content is in a location that the user controls, and they can work with whatever file manager or other tools that they want.