I assume the issue is related to the scoped storage changes introduced in android 11. However the online documentation did not make it clear for me how to approach this now. This is how it was done before:
try {
File ext = Environment.getExternalStorageDirectory();
File newDir = new File(ext "/" "testFile");
if (!newDir.exists()) {
newDir.mkdir();
}
} catch (Exception ex) {
ex.printStackTrace();
}
Keep in mind that this app is already published in the store so tricking the manifest by requesting legacy access is not an option since it probably can't be pulished anymore then. What needs to be changed so I can achieve the same behaviour?
Some background information: The app is creating playlists in that file so if the user switches his device he can copy the whole playlist onto the new device.
CodePudding user response:
Environment.getExternalStorageDirectory()
Change to:
Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOCUMENTS)
And you are done.
CodePudding user response:
You cannot create a new folder in root, you can although create a folder in type specific directories, for example if u want to save pictures in a folder, make a folder in DCIM or Pictures directory, Music for audio files, Documents for , well documents and so on, so basically, if you want to make a new folder instead of
try {
File ext = Environment.getExternalStorageDirectory();
File newDir = new File(ext "/" "testFile");
if (!newDir.exists()) {
newDir.mkdir();
}
} catch (Exception ex) {
ex.printStackTrace();
}
do this
try {
File ext = Environment.getExternalStorageDirectory();
File newDir = new File(ext Environment.DIRECTORY_DOCUMENTS "/" "testFile"); //or any other folder for specific purpose
if (!newDir.exists()) {
newDir.mkdir();
}
} catch (Exception ex) {
ex.printStackTrace();
}