Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 596
FuriousFry Avatar asked Nov 23 '25 10:11

FuriousFry


1 Answers

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

like image 120
xdevs23 Avatar answered Nov 25 '25 23:11

xdevs23