'Is there a way to save an image on DCIM folder on android 9 and below via MediaStore

I am struggling on finding a way to save my image on DCIM folder since MediaStore.Images.Media.RELATIVE_PATH is only available on android 10 and up

Here is a snippet

var imageCollection = sdk29AndUp {
                                MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
                            } ?: MediaStore.Images.Media.EXTERNAL_CONTENT_URI

                            val randomName = UUID.randomUUID().toString()
                            val folderName = context.getString(R.string.output_directory)

                            val contentValues = ContentValues().apply {
                                put(MediaStore.Images.Media.DISPLAY_NAME, "$randomName.jpg")
                                put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")

                                // store in a sub folder on android 10 and up
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                                    put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_DCIM + "/$folderName")
                                } else {
                                    // TODO: How do i set the folder for android 9 and lower

                                }
                            }

try {
                                context.contentResolver.insert(imageCollection, contentValues)?.also { uri ->
                                    cameraLauncher.launch(uri)
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                                // TODO: Notifiy the user there is an error creating the URI
                            }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source