Skip to content Skip to sidebar Skip to footer

Android Java : How To Create Folder Inside Android Devices?

I was developing an app and needed to create a folder inside SD card when button is onClick. I have no idea how to do it. Anyone can help ? Please. @Override protected void onCreat

Solution 1:

3 step:

  1. To get sd card is mounted at /sdcard or any other location by using this way:

    Environment.getExternalStorageDirectory();
    
  2. You have to take uses-permission entry in the AndroidManifest.xml file:

    <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  3. If directory already exists, then mkdir return false. Try creatinng new directory if it not exist:

    Filefolder=newFile(Environment.getExternalStorageDirectory() + "/map");
    booleansuccess=true;
    if (!folder.exists()) {
        success = folder.mkdir();
    }
    if (success) {
        // Do something on success
    } else {
        // Do something else on failure 
    }
    

Solution 2:

Try this code

Filedir=newFile( "dir_name");
  dir.mkdirs();

Solution 3:

Try this..

Filef2=newFile(Environment.getExternalStorageDirectory().toString()+"/Folder");   
f2.mkdirs(); 

And also you have to add write permission in Manifest

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

Solution 4:

// create a File object for the parent directoryFilenameofDirectory=newFile("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
nameofDirectory.mkdirs();
// create a File object for the output fileFileoutputFile=newFile(nameofDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representationFileOutputStreamfos=newFileOutputStream(outputFile);

don't forget to put permission in android manifest

Post a Comment for "Android Java : How To Create Folder Inside Android Devices?"