androidkotlinmediastoreandroid-external-storageinsert-image

How to save bitmap to Storage in KOTLIN (Media.insertImage ==> insertImage is deprecated in Java, also Media.DATA ==> DATA is deprecated in Java)


In KOTLIN, all alternatives have been tested but a bitmap image could not be saved to an External Storage

If I use:

MediaStore.Images.Media.insertImage(this.contentResolver, file.absolutePath, file.name, file.name)

Then, I got "insertImage is deprecated in Java"

If I use:

values.put(MediaStore.Images.Media.DATA, file.absolutePath) context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)

Then, I got " DATA is deprecated in Java "

Also

Environment.getExternalStorageDirectory() is deprecated

How to solve this problem?


Solution

  • Use insert() to obtain an uri. Open an outputstream for the uri and compress your bitmap to it. Android Q?