FuriousFry
FuriousFry

Reputation: 191

Android: mkdir() failed: ENOENT (No such file or directory)

I've been trying to get a file storage system on and android phone to run. However, I've encountered the following problem:

static File dataFolder = Environment.getExternalStorageDirectory();

...

static File userDataFolder = new File(dataFolder, "triathlon");

...

File dayFolder = new File(userDataFolder, folderName);
if(!dayFolder.exists()){
    boolean result = dayFolder.mkdir();
    if (!result){
        Log.d("dayFolder creation", "failed");
    }
}

where folderName is a string representing the current date.

This is the error message:

W/System.err: mkdir failed: ENOENT (No such file or directory) : /storage/emulated/0/triathlon/2016-05-23

I have added the permissions to write and read from external storage. Whats is up and how can I fix this?

Upvotes: 3

Views: 3530

Answers (1)

xdevs23
xdevs23

Reputation: 3994

Instead of

boolean result = dayFolder.mkdir();

use

boolean result = dayFolder.mkdirs();

.mkdirs() will create all necessary parent directories.

One or more parent directories might not exist so you can't create a directory using mkdir(), so you need mkdirs().

Upvotes: 7

Related Questions