I used to save images using MediaStore.Images.Media.insertImage
but insertImage
method is now deprecated. The docs say:
This method was deprecated in API level 29. inserting of images should be performed using MediaColumns#IS_PENDING, which offers richer control over lifecycle.
I don't really get it, since MediaColumns.IS_PENDING
is just a flag, how am I supposed to use it?
Should I use ContentValues
?
SOLVED
The code suggested from @CommonsWare has no problem, except the fact that if you are programming with targetSdkVersion 29
, you must add the condition:
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, System.currentTimeMillis().toString())
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { //this one
put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation)
put(MediaStore.MediaColumns.IS_PENDING, 1)
}
}