简体   繁体   中英

Android Cannot create new folder

I cannot create new folder any way, it always return false when use mkdirs. I am using Android 6.0.1 in android studio debug mode.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />`

String folder_main = "NewFolder";
        Boolean success = false;
        File f = new File(Environment.getExternalStorageDirectory(),folder_main
                );
        if (!f.exists()) {
            Log.d("path","not exist");
            success=f.mkdirs();
        }
        else
        {
            Log.d("path","exist");
        }

        Log.d("path",success.toString());

I didn't use write permission since in this docs

Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by getExternalFilesDir(String) and getExternalCacheDir().

The doc says you can read/write files only in the directories returned by getExternalFilesDir(String) and getExternalCacheDir() without write permission. But you get directory path by Environment.getExternalStorageDirectory().

Javadoc of Environment.getExternalStorageDirectory() says.

Writing to this path requires the WRITE_EXTERNAL_STORAGE permission, and starting in KITKAT, read access requires the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.

Starting in KITKAT, if your application only needs to store internal data, consider using getExternalFilesDir(String), getExternalCacheDir(), or getExternalMediaDirs(), which require no permissions to read or write.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM