Skip to content Skip to sidebar Skip to footer

Create File Failed From Mkdir()

I try to create file in my android emulator, but when i finish my code. I can't find the file i create from android device monitor. Here is my code: try { if (Environment.getEx

Solution 1:

you should replace

dirFile.mkdir();

to

dirFile.mkdirs();

Example:

if (!dirFile.exists()) { // if file doesn't exist
        System.out.println(dirFile.mkdir());
        System.out.println(dirFile.mkdirs());

    }

will yield false for the first [and no dir will be created], and true for the second, and you will have created /demos/file/tmp/test

mkdirs() also creates parent directories in the path this File represents.

javadocs for mkdirs():

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

javadocs for mkdir():

Creates the directory named by this abstract pathname.

hope help you!

Solution 2:

For Android 6+ you need to add code to ask the user to confirm the permissions you asked in manifest.

Google for runtime permissions.

Solution 3:

I had the same problem a few months ago.

In my case the file was there, but windows couldn't see it (or the integration between adb and windows explorer).

Resetting the MOBILE was the only solution I found... After the reset the file was there in Windows Explorer.

I recommend you to try to find your files using the cli command:

adb shell ls sdcard/"your_path"

Also, try to create it using:

.getAbsolutePath()

Like this:

File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "YOUR_FOLDER_NAME");

if(!path.exists()) 
{
    path.mkdirs();
} 
else 
{ 
    Log.d ("TAG","Path already exists");
}

Hope it helps

Post a Comment for "Create File Failed From Mkdir()"