Directory creation in / sdcard is not performed

I am trying to create a directory in /sdcard programmatically, but it does not work. The code below always displays directory not created.

 boolean success = (new File("/sdcard/map")).mkdir(); if (!success) { Log.i("directory not created", "directory not created"); } else { Log.i("directory created", "directory created"); } 
+53
android file directory io
04 Oct 2018-10-10T00:
source share
11 answers

There are three things here:

  • Do not assume that the SD card is mounted on /sdcard (it may be true in the default case, but it is better not to hardcode). You can get the location of the SD card by querying the system:

     Environment.getExternalStorageDirectory(); 
  • You must tell Android that your application should write to external storage by adding a use-permission entry to the AndroidManifest.xml file:

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
  • If this directory already exists, then mkdir will return false. Therefore, check for the existence of a directory and try to create it if it does not exist. In your component, use something like:

     File folder = new File(Environment.getExternalStorageDirectory() + "/map"); boolean success = true; if (!folder.exists()) { success = folder.mkdir(); } if (success) { // Do something on success } else { // Do something else on failure } 
+146
04 Oct 2018-10-10T00:
source share
β€” -

I had the same problem after I upgraded my Android phone to 6.0 (API level 23). The following solution works for me. Hope this helps you.

Please check your Android version. If it's> = 6.0 (API level 23), you need to not only enable

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

in your AndroidManifest.xml, but also request permission before calling mkdir () . Snopshot code.

 public static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1; public int mkFolder(String folderName){ // make a folder under Environment.DIRECTORY_DCIM String state = Environment.getExternalStorageState(); if (!Environment.MEDIA_MOUNTED.equals(state)){ Log.d("myAppName", "Error: external storage is unavailable"); return 0; } if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { Log.d("myAppName", "Error: external storage is read only."); return 0; } Log.d("myAppName", "External storage is not read only or unavailable"); if (ContextCompat.checkSelfPermission(this, // request permission when it is not granted. Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { Log.d("myAppName", "permission:WRITE_EXTERNAL_STORAGE: NOT granted!"); // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Show an expanation to the user *asynchronously* -- don't block // this thread waiting for the user response! After the user // sees the explanation, try again to request the permission. } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. } } File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),folderName); int result = 0; if (folder.exists()) { Log.d("myAppName","folder exist:"+folder.toString()); result = 2; // folder exist }else{ try { if (folder.mkdir()) { Log.d("myAppName", "folder created:" + folder.toString()); result = 1; // folder created } else { Log.d("myAppName", "creat folder fails:" + folder.toString()); result = 0; // creat folder fails } }catch (Exception ecp){ ecp.printStackTrace(); } } return result; } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } // other 'case' lines to check for other // permissions this app might request } } 

For more information, please read " Requesting Permissions at Runtime "

+21
Aug 01 '16 at 8:19
source share

The correct path to the SD card is

 /mnt/sdcard/ 

but as said earlier, you should not hard code it. If you are on Android 2.1 or later, use

 getExternalFilesDir(String type) 

Otherwise:

 Environment.getExternalStorageDirectory() 

Read carefully https://developer.android.com/guide/topics/data/data-storage.html#filesExternal

In addition, you will need to use this method or something similar.

 boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { // Something else is wrong. It may be one of many other states, but all we need // to know is we can neither read nor write mExternalStorageAvailable = mExternalStorageWriteable = false; } 

then check if you can access the SD card. As said, read the official documentation.

Another option, maybe you need to use mkdirs instead of mkdir

 file.mkdirs() 

Creates a directory named by the final file name of this file, including the full directory path required to create this directory.

+14
04 Oct 2018-10-10T00:
source share

Reboot your Android device. When I restarted the device, I started working for me.

+13
Jul 08 '13 at 13:16
source share

If this happens with Android 6 and compiles target> = 23, don't forget that now we use runtime permissions . Therefore, granting permissions in the manifest is no longer enough.

+7
Mar 03 '16 at 15:42
source share

use mkdirs () instead of mkdir () .. this worked for me :)

 File folder = new File(Environment.getExternalStorageDirectory()+"/Saved CGPA"); if(!folder.exists()){ if(folder.mkdirs()) Toast.makeText(this, "New Folder Created", Toast.LENGTH_SHORT).show(); } File sdCardFile = new File(Environment.getExternalStorageDirectory()+"/Saved CGPA/cgpa.html"); 
+6
Oct 30 '13 at 18:10
source share

in android api> = 23

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

instead

  <app:uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+2
Dec 27 '16 at 21:10
source share

Do you have rights to write to the SD card in the manifest? Find WRITE_EXTERNAL_STORAGE at http://developer.android.com/reference/android/Manifest.permission.html

+1
04 Oct '10 at 7:56
source share

Isn't it already created? Mkdir returns false if the folder already exists mkdir

+1
04 Oct 2018-10-10T00:
source share

I made a mistake by including both:

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

and

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

in the above order. So when I took out the second resolution (READ), the problem disappeared.

0
Jan 31 '14 at 15:22
source share
 File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/FoderName"); if (!f.exists()) { f.mkdirs(); } 
0
Aug 09 '15 at 8:44
source share



All Articles