我在清单中设置了权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
我尝试使用以下代码创建目录:
// test external storage root
String myDir = Environment.getExternalStorageDirectory().toString();
File f = new File(myDir);
if(f.exists()) {
Log.d("MyApp", "folder exists: " + f.toString());
}else {
Log.d("MyApp", "folder does not exist: " + f.toString());
}
// try create folder
myDir += File.separator + "MyFolder";
f = new File(myDir);
if(f.exists()) {
Log.d("MyApp", "folder exists: " + f.toString());
}else{
Log.d("MyApp", "folder does not exist: " + f.toString());
if(f.mkdirs()){
Log.d("MyApp", "folder created: " + f.toString());
}else{
Log.d("MyApp", "cannot create: " + f.toString());
}
}
Android 4和5的平板电脑是功能上的,但在Android 6.0的Nexus 5X上看起来像:
D/MyApp: folder exists: /storage/emulated/0
D/MyApp: folder does not exist: /storage/emulated/0/MyFolder
D/MyApp: cannot create: /storage/emulated/0/MyFolder
在Nexus 5X或Android 6.0中有一些我不知道的甜蜜秘诀吗?