ollidiemaus
ollidiemaus

Reputation: 279

creating a folder on sd with subfolders

I got a problem with the following code:

    File folder = new File(Environment.getExternalStorageDirectory() + "/myapp/folderone/foldertwo");
    boolean success = false;
    if (!folder.exists()) {
        success = folder.mkdir();
    }
    if (!success) {
    } else {
    }

but its simply not working I also added the permission:

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

Hope someone could help me with this.

Upvotes: 6

Views: 5402

Answers (2)

Thkru
Thkru

Reputation: 4199

Try to use mkdirs() instead of mkdir() only, this worked for me.

Example:

File folder = new File(Environment.getExternalStorageDirectory() + "/myapp/folderone/foldertwo");
    boolean success = false;
    if (!folder.exists()) {
        success = folder.mkdirs();
    }
    if (!success) {
    } else {
    }

Upvotes: 24

dymmeh
dymmeh

Reputation: 22306

Have you tried calling mkdirs() instead of mkdir()?

mkdir will only create the single folder specified. In your case "foldertwo".

mkdirs will create the folder specified (foldertwo) along with all other required folders in the path (myapp & folderone)

Upvotes: 6

Related Questions