Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 546
Daniele Avatar asked Sep 29 '17 13:09

Daniele


People also ask

What's in Android Oreo?

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).

How do I get my App to work on Android Oreo?

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.

What is external storage in Android example?

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.

What is custom data store in Android 8?

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.


1 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");
like image 70
CommonsWare Avatar answered Sep 22 '22 13:09

CommonsWare