Android Oreo (API 26) - Create dir in external storage

Viewed 5514

I've been developing an app on nougat that creates a directory in the external storage.

I used to do it like this:

final File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Chords/Processed Audio");
dir.mkdirs();

This code does not seem to work on API 26 (Android Oreo). The directory is not created.

How can I achieve the same thing, preferably that works on all android version from API 21 to API 26?

2 Answers

I have no problems running your existing code on a Nexus 5X running Android 8.0. Using adb shell ls /storage/emulated/0, I see Chores/, and inside there I see Processed Audio/. This is for an app with WRITE_EXTERNAL_STORAGE permission, including runtime permissions.

That being said, ideally, do not use string concatenation to create File objects. Instead, use:

final File dir = new File(new File(Environment.getExternalStorageDirectory(), "Chords"), "Processed Audio");
Related