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?

2

There are 2 best solutions below

15
On BEST ANSWER

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

1
On

If you want to trully understand what you need you should look here.
But if you worried that Environment.getExternalStorageDirectory() is deprecated you can use Environment.getRootDirectory() or Environment.getRootDirectory()