For public media folders instead of Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
we can use:
values.put(MediaStore.Media.RELATIVE_PATH, Environment.DIRECTORY_DCIM)
uri = resolver.insert(
MediaStore.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY),
values
)
But what we can use instead of Context.externalMediaDirs
then?
Or private media folders of each app are deprecated right now and should not be used at all?
Update
I guess yes because now we have MediaStore.getExternalVolumeNames(context)
with Android SDK 29 which can even return volume name of removable MicroSD card and save media files to public media folders.
We could not do it on Android on previous SDK versions for a very long time and now it's possible (weird)
On previous Android SDKs we could use Context.externalMediaDirs[1]
to get private app media path of removable MicroSD card, but retrieving public media folder wasn't possible
But it's so bad that we have to support at least SDK 21+ and have a lot of code to implement app which will support secondary storage (removable MicroSD card) for all versions
Update 2
Here's an easy example (hardcoded [0]
and [1]
are just for easy explanation of course):
Android 29+:
val volumeNames = MediaStore.getExternalVolumeNames(context).toTypedArray()
val phoneSdCard: String = volumeNames[0]
// external_primary == /storage/emulated/0/DCIM
val removableMicroSdCard: String = volumeNames[1]
// 12f7-270f == /storage/12F7-270F/DCIM
// which are used to work with MediaStore:
values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_DCIM)
uri = resolver.insert(
MediaStore.Images.Media.getContentUri( phoneSdCard OR removableMicroSdCard ),
values
)
Android 21-28:
val phoneSdCard: File = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
// /storage/emulated/0/DCIM
val removableMicroSdCard: File = context.externalMediaDirs[1]
// /storage/12F7-270F/Android/media/com.example.app
// so on older versions there is no way to write media files at /storage/12F7-270F/DCIM