Using SAF I selected a folder for R/W media files.
For MediaStore.Audio.Media.getContentUri(volumeName)
(29+ API) I need to specify a volume name (it can be a primary storage or external sd card) and then specify relative path using at:
val contentValues = ContentValues().apply {
put(
MediaStore.Audio.Media.RELATIVE_PATH,
relativePath
)
}
For example I have the following URIs (uriFolder
):
content://com.android.externalstorage.documents/tree/primary%3AMUSIC
(primary storage)content://com.android.externalstorage.documents/tree/18F0-2D1C%3AMusic
(external sd card)so basically volume name can be "external_primary"
(primary built-in storage) or "18F0-2D1C"
(an external sd card)
I can just parse this string and get volume name:
a) if "primary"
then it's going to be "external_primary"
(we also have the following constant public static final String VOLUME_EXTERNAL_PRIMARY = "external_primary";
at MediaStore
class)
b) if it's an external sd card then just get "18F0-2D1C"
from string.
But there is also the following function: MediaStore.getVolumeName(uri)
(29+ API)
I thought it would give me the same results instead of parsing the URI path string manually:
val volumeName = MediaStore.getVolumeName(uriFolder)
But I get the following exception:
java.lang.IllegalArgumentException: Missing volume name: content://com.android.externalstorage.documents/tree/primary%3AMUSIC
Though for relativePath
I still need to parse URI path string manually to get "MUSIC"
after %3A
So it's quite tricky to work with MediaStore
and SAF
together and fully stop using File
classes
MediaStore.getVolumeName(uri) works only for uris you got from the media store.
Such uries start with content://media/...
MediaStore.getVolumeName(uri): external for: content://media/external/file/1000469026
MediaStore.getVolumeName(uri): 28ef-1de5 for: content://media/28ef-1de5/file/391988