0

I am using this code to save a picture to a specific directory. However, since I'm targeting SDK 31, getExternalStorageDirectory() is now deprecated. Can someone please provide new alternative code to the one below? Access to that specific path is granted already.

val mIcon = BitmapFactory.decodeResource(getResources(), R.drawable.aztec)
File(Environment.getExternalStorageDirectory().toString() + "/specific/path", "output.jpg").writeBitmap(mIcon, Bitmap.CompressFormat.JPEG, 85) 

Access to "/specific/path" is already granted previously using takePersistableUriPermission.

.writeBitmap method:

private fun File.writeBitmap(bitmap: Bitmap, format: Bitmap.CompressFormat, quality: Int) {
    outputStream().use { out ->
        bitmap.compress(format, quality, out)
        out.flush()
    }
}
iamhere
  • 1
  • 1
  • 11
  • "since I'm targeting SDK 31, getExternalStorageDirectory() is now deprecated" -- [that is now undeprecated](https://commonsware.com/blog/2021/11/06/about-environment-undeprecations.html). – CommonsWare May 04 '22 at 12:04
  • @CommonsWare Thank you for your answer, it's truly appreciated! I thought that deprectation was the cause of [this issue](https://stackoverflow.com/questions/72081446/error-when-writing-bitmap-to-directory-even-with-directory-access) as suggested by an answer, but it isn't according to you. I was wondering if you could help me with this question? – iamhere May 04 '22 at 13:40
  • Use getExternalPublicStorageDirectory() instead. – blackapps May 04 '22 at 14:39

0 Answers0