'One Plus Nord N10 5g Does not always save the Photo taken by Camera Intent using Media Store URI

I am developing an app that takes a photo using the Default camera app of phones utilizing the Camera Intent to take picture. I am creating the URI to be passed on the camera intent via Media Store here is a snippet

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val imageCollection =
                MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)

            val contentValues = ContentValues().apply {
                put(
                    MediaStore.Images.Media.DISPLAY_NAME,
                    "$randomName.jpg"
                )
                put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
                put(
                    MediaStore.Images.Media.RELATIVE_PATH,
                    Environment.DIRECTORY_PICTURES + "/$folderName"
                )
            }

            try {
                context.contentResolver.insert(
                    imageCollection,
                    contentValues
                )
                    ?.also { uri ->
                        return@withContext uri
                    }
            } catch (e: Exception) {
                e.printStackTrace()
                return@withContext null
            }
        }

Sometimes it works but most of the time it does not save the photo. When it does not save the photo the intent result is Activity.RESULT_OK.

This only happens in Oneplus Nord N10 5g



Sources

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

Source: Stack Overflow

Solution Source