I am trying to save an image taken from the camera using the following codes:
@RequiresApi(Build.VERSION_CODES.Q)
private fun setImageUri(): Uri {
val resolver = contentResolver
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, "house2.jpg")
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
put(MediaStore.MediaColumns.RELATIVE_PATH, "Pictures/OLArt")
}
imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
return imageUri!!
}
The function works well for the first time. however when the image (house2.jpg) already exists, the system will create another file called "house2 (1).jpg", "house2 (2).jpg, etc (instead of replacing the old file)
is there anything I can set in the contentValues to force the resolver to replace the file rather than create copies of it?
below is the codes for the take picture intent.
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri()) //<- i paste in the imageUri here
// Ensure that there's a camera activity to handle the intent
takePictureIntent.resolveActivity(packageManager)?.also {
startActivityForResult(takePictureIntent, 102)
}
}
