MediaStore.Images.Media.insertImage deprecated

☆樱花仙子☆ 提交于 2020-03-18 06:46:18

问题


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 ?


回答1:


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)
            }
        }


来源:https://stackoverflow.com/questions/57726896/mediastore-images-media-insertimage-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!