Edmund Rojas
Edmund Rojas

Reputation: 6606

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" 

Upvotes: 5

Views: 4863

Answers (3)

andreikashin
andreikashin

Reputation: 1605

Do not forget to ask for WRITE_EXTERNAL_STORAGE permission explicitly. Here is how to do that: https://developer.android.com/training/permissions/requesting

Upvotes: 0

simmash
simmash

Reputation: 31

I was stuck with issue of folder not getting created for 2 days. Code was perfect, tried multiple code options but none worked. Ultimately I rebooted by device - Nexus 4 and reconnected to my Windows 7 laptop for debugging. I was able to see all files and the directory in Windows 7 explorer. Looks like it was just issue with Nexus 4 (kitkat) not refreshing the folder structure and it was also not visible on Windows 7 explorer and was not working with my app. Also after restart of Nexus 4 the error from call mediaRecorder.start() "not able to start" also disappeared and all files and directories my app is creating appeared.

Upvotes: 0

Mauro
Mauro

Reputation: 381

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.

Upvotes: 9

Related Questions