3

Working on android Java, recently updated SDK to API level 29 now there is a warning shown which states that

Environment.getExternalStorageDirectory() is deprecated in API level 29

My code is

 val wallpaperDirectory = File(Environment.getExternalStorageDirectory(), "NewFolder")
    wallpaperDirectory.mkdir()

How to create newFolder in intenal storage

Nozim Rustamov
  • 41
  • 1
  • 2
  • 5
  • Does this answer your question? [getExternalStoragePublicDirectory deprecated in Android Q](https://stackoverflow.com/questions/56468539/getexternalstoragepublicdirectory-deprecated-in-android-q) – SaadAAkash Jan 27 '20 at 09:38

2 Answers2

3

You can access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

read :-https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

check this demo

void createExternalStoragePrivateFile() {
    // Create a path where we will place our private file on external
    // storage.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");

    try {
       // your code
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }
}

void deleteExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    file.delete();
}

boolean hasExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    return file.exists();
}
Jaydeep chatrola
  • 2,423
  • 11
  • 17
-1

From API 29 onwards they deprecated this API for security reasons. However you can access these api on Context , then it will work. Dont use environment. Refer: https://developer.android.com/reference/android/os/Environment for further details on same.

Manisha
  • 1
  • 2