java – Android无法在外部存储上创建新目录

由于某种原因无法在Android外部存储中创建新目录,我假设有一些东西我缺少但不能在下面发现它是我用来尝试创建目录的代码,任何帮助都会有很长的路要走

Calendar calendarTime = Calendar.getInstance();

        String newFolder = "/NewFolder/videos";

        File newDirectory = new File(Environment
                .getExternalStorageDirectory().toString() + newFolder);

        newDirectory.mkdir();

        File file = new File(newDirectory,
                String.valueOf(calendarTime.getTimeInMillis()) + ".mp4");

        if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.THREE_GPP) {

            recorder.setOutputFile(file.getAbsolutePath());
        } else if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.MPEG_4) {

            recorder.setOutputFile(file.getAbsolutePath());
        } else {

            recorder.setOutputFile(file.getAbsolutePath());

        }

在我的清单中

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 

解决方法:

File.mkdir()只会在父目录存在的情况下创建目录.在这种情况下,您要创建的目录是“视频”,其父目录是“NewFolder”.如果“NewFolder”不存在,则不会创建“视频”.如果要一次创建两个目录,则应使用File.mkdirs().

上一篇:文件夹管理


下一篇:linux -备份\还原系统