0

I have stuck up with creation of folder in my mobile which is (Micromax Canvas 2).I cant able to create folder.please tel me where i made mistake.

File folder = new File(Environment.getExternalStorageDirectory() + "/Example");
        boolean success = true;
        if (!folder.exists()) {
            success = folder.mkdirs();
        }
        if (success) {
          Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_LONG).show();
        }

permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Giridharan
  • 4,402
  • 5
  • 27
  • 30

6 Answers6

3
File f1 = new File(getApplicationContext().getFilesDir()+"");
fol = new File(f1, "Images");
if(!fol.exists())
{
    fol.mkdir();
}
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Vijay Barbhaya
  • 876
  • 1
  • 6
  • 14
1
 File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Example");
    boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdirs();
    }
    if (success) {
      Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_LONG).show();
    }

Use getExternalStorageDirectory().getAbsolutePath() instead of just getExternalStorageDirectory().

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
1

I implement like this. Instead of "plus(+)" write with a "comma(,)"

 File imageFileFolder = new File(Environment.getExternalStorageDirectory(),
                                 "folder name");
 imageFileFolder.mkdir();
canova
  • 3,965
  • 2
  • 22
  • 39
1

At first add permission in AndroidMenifest.xml file

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

then add FolderInfo.Class

package com.xxx.cg;

    import java.io.File;

    import android.os.Environment;

    public class FolderInfo {
        public static final String SDCARD;
        static {
            SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        public static final String CG_FOLDER = SDCARD + "/CG";

        public static String ASSET_FOLDER = CG_FOLDER + "/assets";

        public static boolean createFolderForCG() {
            boolean exist = false;
            File dir = new File(CG_FOLDER);
            if (dir.exists()) {
                exist = true;
            } else {
                if (dir.mkdirs()) {
                    exist = true;
                }
            }
            return exist;
        }

        public static boolean createAssetsFolderForCG() {
            boolean exist = false;
            File dir = new File(ASSET_FOLDER);
            if (dir.exists()) {
                exist = true;
            } else {
                if (dir.mkdirs()) {
                    exist = true;
                }
            }
            return exist;
        }

        public static boolean createFolder(String folder) {
            boolean exist = false;
            File dir = new File(ASSET_FOLDER + "/" + folder);
            if (dir.exists()) {
                exist = true;
            } else {
                if (dir.mkdirs()) {
                    exist = true;
                }
            }
            return exist;
        }

    }

then call from your activity. such as MainActivity.Class.

        FolderInfo.createFolderForCG();
    FolderInfo.createAssetsFolderForCG();
            FolderInfo.createFolder(subFolderName);

then run. You can show CG/assets your SD Card. and also sub folders show CG/assets/.................

Best of Luck!

0
String downloadDirectory = "/folderName";
        String extStorageDirectory = Environment.getExternalStorageDirectory()
                .toString();
        File newDownloadDirectory = new File(extStorageDirectory
                + downloadDirectory);
        newDownloadDirectory.mkdir();
Android
  • 8,995
  • 9
  • 67
  • 108
0

I have solved the problem.. I have declared <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> after the application tag.So it didnt works. I declared permission before application tag.Now folder had been created.

Giridharan
  • 4,402
  • 5
  • 27
  • 30