在某些Android版本上创建目录

在某些Android版本上创建目录,android,android-permissions,Android,Android Permissions,我用Genymotion棉花糖和牛轧糖在我的HTC 10上测试了这段代码,它对这两种产品都有效。 现在我在Genymotion上尝试了Android 7.0,但它没有创建目录 知道为什么吗 File file = new File(Environment .getExternalStorageDirectory() + File.separator + "SchoolAssist" + File.separator + lesson_name); boolean isDir =

我用Genymotion棉花糖和牛轧糖在我的HTC 10上测试了这段代码,它对这两种产品都有效。
现在我在Genymotion上尝试了Android 7.0,但它没有创建目录

知道为什么吗

File file = new File(Environment
    .getExternalStorageDirectory() + File.separator +
    "SchoolAssist" + File.separator + lesson_name);

boolean isDir = file.exists();
if (!isDir)
    isDir = file.mkdirs();

if (isDir) {
    Intent notes = new Intent(getActivity(), NotesManager.class);
    notes.putExtra("dir", file.getAbsolutePath());
    startActivity(notes);
}
else
    Toast.makeText(getContext(), "Error creating directory", Toast.LENGTH_SHORT).show();
编辑:我的清单包含以下行:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

在代码中,即使在创建目录时,也会显示Toast。只有在调用代码之前文件已经存在时,活动才会启动

试试这个:

File file = new File(Environment
            .getExternalStorageDirectory() + File.separator +
            "SchoolAssist" + File.separator + lesson_name);

if(!file.exists()) {
    if(file.mkdirs()) {
        startNotesManager();
    } else {
        Toast.makeText(getContext(), "Error creating directory", Toast.LENGTH_SHORT).show();
    }
} else {
    startNotesManager();
}
并实现此帮助器方法来启动活动:

private void startNotesManager() {
    Intent notes = new Intent(getActivity(), NotesManager.class);
    notes.putExtra("dir", file.getAbsolutePath());
    startActivity(notes);
}

是否可能是您忘记授予最后一台设备的权限?我不需要授予任何设备的权限您需要自己在
API>=23
-