Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android unable to create new directory on external storage

Unable to create a new directory in Android external storage for some reason, Im assuming there is something im missing but not able to spot it below is my code which I am using to attempt to create the directory, any help will go a long way thanks

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());

        }

and in my manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
like image 211
Edmund Rojas Avatar asked Jun 10 '13 19:06

Edmund Rojas


1 Answers

File.mkdir() will only create the directory if its parent exists. In this case, the directory you want to create is "videos" and its parent is "NewFolder". "videos" will not be created if "NewFolder" does not exist. If you want to create both directories at once, you should use File.mkdirs() instead.

like image 51
Mauro Avatar answered Sep 19 '22 14:09

Mauro