2

I am trying to generate a file name to save an AR Model using ARCore and Kotlin.

Since Android 10 the Environment methods getExternalStoragePublicDirectory() and getExternalStorageDirectory() were deprecated for privacy concerns.

What can be a replacement for the following code?

private fun generateFileName() : String {
        val date = SimpleDateFormat("yyyyMMddHHmmss", java.util.Locale.getDefault()).format(Date())
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "CardAR/" + date + "_screenshot.jpg"
    }
buckBoy
  • 98
  • 1
  • 8

2 Answers2

1
private File getAbsoluteFile(String relativePath, Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        return new File(context.getExternalFilesDir(null), relativePath);
    } else {
        return new File(context.getFilesDir(), relativePath);
    }
}

This method will return the full path to the file.

Destroyer
  • 785
  • 8
  • 20
1

Instead of using Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) you can now use getExternalFilesDir(Environment.DIRECTORY_PICTURES)

Here are a few answers that might help you.

Biscuit
  • 4,840
  • 4
  • 26
  • 54