Search code examples
androidmkdir

Android: mkdir not working


I want to create MyFolder directory , but cannot to do it, tried different way, but no one working; In Androidmanifest.xml i have:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package" >
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.android.vending.BILLING" />

    <application
    <!---.....-->
</manifest>

static class:

public static String FILES_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "MyFolder";

Code:

File dir = new File(Constants.FILES_DIR);
if (!dir.exists()) {
    dir.mkdir();
    Log.i("my tag", "isDirectory + " + dir.toString() + " | " + dir.exists() + " | " + dir.mkdir());
    dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "MyFolder");
    Log.i("my tag", "isDirectory + " + dir.toString() + " | " + dir.exists() + " | " + dir.mkdir());
    dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "MyFolder/");
    Log.i("my tag", "isDirectory + " + dir.toString() + " | " + dir.exists() + " | " + dir.mkdir());

    Log.i("my tag", "Status + " + Environment.getExternalStorageState());
    if (!dir.isDirectory()) return false;
}

And logcat:

    05-09 14:40:15.131    1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/myFolder | false | false
05-09 14:40:15.131    1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/myFolder | false | false
05-09 14:40:15.136    1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/Music/myFolder | false | false
05-09 14:40:15.136    1453-1453/my.package I/my tag﹕ Status + mounted
05-09 14:40:17.261    1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/myFolder | false | false
05-09 14:40:17.261    1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/myFolder | false | false
05-09 14:40:17.261    1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/Music/myFolder | false | false
05-09 14:40:17.266    1453-1453/my.package I/my tag﹕ Status + mounted

I also tried mkdirs();, path with slash and without, Google it..and finally I'm desperate

When i manually crate folder and tried to save file into, i recieve permission error - open failed: EACCES (Permission denied)


Solution

  • In one of my open source projects i use the following class to get the SD Card directory. This is just a simple helper and it should be easy to modify it for any other SD Card directory, or special directory.

    /**
     * http://developer.android.com/training/basics/data-storage/files.html
     *
     * @param albumName
     * @return
     */
    public static File getAlbumStorageDir(String albumName) {
        // Get the directory for the user's public pictures directory.
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);
        if (!file.mkdirs()) {
            //This is also the case if the directory already existed
            Log.i("wall-splash", "Directory not created");
        }
        return file;
    }
    

    As of creating a file i have the following code:

    File dir = Utils.getAlbumStorageDir("wall-splash")
    //create the file
    File file = new File(dir, fileName);
    if (!file.exists()) {
        file.createNewFile();
    }
    

    What is the device you are testing on? Does it have external storage? Any custom rom?


    Try to reboot your device, to make sure nothing is blocked from a previous try.