buckBoy
buckBoy

Reputation: 358

getExternalStorageDirectory deprecated in Android 10

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

Upvotes: 2

Views: 8271

Answers (2)

Biscuit
Biscuit

Reputation: 5247

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.

Upvotes: 1

Destroyer
Destroyer

Reputation: 795

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.

Upvotes: 1

Related Questions