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?
What's in Android Oreo? Android Oreo gives you many new ways to extend your app and develop more efficiently. Target Android Oreo (API 26 or 27) and extend your apps with the latest platform capabilities and APIs. Highlights of features and APIs for your apps (API 26).
Just download a device system image, install your current app, and test in areas where behavior changes may affect the app. Update your code and publish, using the app's current platform targeting. Run Android Oreo on your test device. Easy steps to reach compatibility. System changes that may affect your app on Android Oreo.
External Storage in Android with Example. Android gives various options for storing apps data which uses a file system similar to the disk-based system on computer platforms. App-Specific storage: Store data files within internal volume directories or external. These data files are meant only for the app’s use.
Android 8.0 (API level 26) lets you provide a custom data store to your preferences, which can be useful if your app stores the preferences in a cloud or local database, or if the preferences are device-specific. For more information about implementing the data store, refer to Custom Data Store.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With