Create to a folder under /sdcard/Download folder in Android API 29 and API 30

Viewed 958

I use this code to make a folder under /sdcard/Download

String toLocation = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + 
                + "/MyFolder/example.txt";
File toLocationFile = new File(toLocation);

if (!toLocationFile.getParentFile().exists()) {
    toLocationFile.getParentFile().mkdirs();
}

The build is targeted to API 30. The code can create folder "MyFolder" in Android 11 device but can't in Android 10 device.

Why does the same call get different result?

Notes:

Not set requestLegacyExternalStorage = true in AndroidManifest; means I am using scoped storage.

No write permission.

1 Answers

In an Android 11 device you do not need any permission to create that folder.

For an Android 10 device add requestLegacyExternalStorage="true" or use Storage Access Framework or the DownloadManager.

Related