Search code examples
androidcameradirectoryphotomkdir

Cannot mkdir folder in Android


I am having problem to running function Mkdir(). What should I do? I already added read/write permission:

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

Below is my code snippet:

private File getTempFile(Context context)
    {

        File path;
        //Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){

            path=new File(android.os.Environment.getExternalStorageDirectory(),"/Tanker/Foto/");
        }
        else {
            path=context.getCacheDir();
        }
        String pictureFileName = Long.toHexString(System.currentTimeMillis())+".jpg";
        if (!path.exists())
        {
            path.mkdir();
        }
        return new File(path, pictureFileName);
    }

please help.


Solution

  •  path=new File(android.os.Environment.getExternalStorageDirectory(),"/Tanker/Foto/");
    

    mkdir will not create the complete path. You have to use mkdirs for that

    From the documentation

    Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.

    e.g.

        if (!path.exists()) {
            path.mkdirs();
        }