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?
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");
@Daniele,请参阅https://stackoverflow.com/a/44455957/966789和https://stackoverflow.com/a/33031091/966789(Android为Android 6.0添加了新的权限模型(Marshmallow)。如果你的targetSdkVersion> = 23如果您在Marshmallow(或更高版本)设备上运行,则可能需要启用运行时权限。您还应该阅读有关运行时权限更改的更多信息。如果您使用的是targetSdkVersion> = 24,则还必须将FileProvider配置为show in以下示例使用com.codepath.fileprovider并且应该与指定的权限XML标记匹配)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.