简体   繁体   中英

How to create a directory in External SD in android application

I want to create a directory on "/mnt/extsd/MyFolder" this path. while calling mkdir() it returns false.I inserted the sdcard on my tablet, got the external path as "/mnt/extsd" and trying to create a folder on this path. Below is my code,

File lSDCardDirFile = new File("/mnt/extsd/MyFolder");
    if (!lSDCardDirFile.exists()) {
        System.out.println("Is folder created --- " + lSDCardDirFile.mkdirs());
    }

I gave the permissions, . I want to create the folder in External sd card which is removable sd card. I am using android 4.0 ICS version device.

I created a different method for getting paths fom external SD card,

  public static String[] getStorageDirectories()
  {
      String[] lDirs = null;
      BufferedReader lBufferReader = null;
      try {
          lBufferReader = new BufferedReader(new FileReader("/proc/mounts"));
          ArrayList list = new ArrayList();
          String lStrline;
          while ((lStrline = lBufferReader.readLine()) != null) {
              if (lStrline.contains("vfat") || lStrline.contains("/mnt")) {
                  StringTokenizer lTokenizer = new StringTokenizer(lStrline, " ");
                  String lStrPath = lTokenizer.nextToken();
                  lStrPath = lTokenizer.nextToken(); // Take the second token, i.e. mount point

                  if (lStrPath.equals(Environment.getExternalStorageDirectory().getPath())) {
                      list.add(lStrPath);
                  }
                  else if (lStrline.contains("/dev/block/vold")) {
                      if (!lStrline.contains("/mnt/secure") && !lStrline.contains("/mnt/asec") && !lStrline.contains("/mnt/obb") && !lStrline.contains("/dev/mapper") && !lStrline.contains("tmpfs")) {
                          list.add(lStrPath);
                      }
                  }
              }
          }

          lDirs = new String[list.size()];
          for (int i = 0; i < list.size(); i++) {
              lDirs[i] = (String) list.get(i);
          }
      }
      catch (FileNotFoundException e) {}
      catch (IOException e) {}
      finally {
            if (lBufferReader != null) {
                try {
                    lBufferReader.close();
                } catch (IOException e) {
                }
            }
            }

      return lDirs;
  }`

From this method I got the path, but while trying to create a directory, the mkdir() returns false.

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

I have two folders like extSdCard and sdcard in my samsung galaxy s3.

Use the below code to choose.

private String[] mFilePaths;

File storageDir = new File("/mnt/");
if(storageDir.isDirectory()){
    File[] dirList = storageDir.listFiles();
    for (int i = 0; i < dirList.length; i++)
    {
        mFilePaths[i] = dirList[i].getAbsolutePath();
        System.out.println("...................................."+mFilePaths[i]);
    }
}

File Dir;
if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))//check if sd card is mounted
{
    Dir=new File(android.os.Environment.getExternalStorageDirectory(),"your folder name");

    if(!Dir.exists())// if directory is not here
        Dir.mkdirs() // make directory
}

Edit

To get the paths of internal and external storage. below code works on samsung galaxy s3.

String externalpath = new String();
String internalpath = new String();

public  void getExternalMounts() {
    Runtime runtime = Runtime.getRuntime();
    try
    {
        Process proc = runtime.exec("mount");
        InputStream is = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        String line;

        BufferedReader br = new BufferedReader(isr);
        while ((line = br.readLine()) != null) {
            if (line.contains("secure")) continue;
            if (line.contains("asec")) continue;

            if (line.contains("fat")) {//external card
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                    externalpath = externalpath.concat("*" + columns[1] + "n");
                }
            }
            else if (line.contains("fuse")) {//internal storage
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                    internalpath = internalpath.concat(columns[1] + "n");
                }
            }
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    System.out.println("Path  of sd card external............"+externalpath);
    System.out.println("Path  of internal memory............"+internalpath);
}

Now you can use the path os external storage to create a folder.

if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))//check if sd card is mounted
{
    Dir=new File(externalpath,"your folder name");

    if(!Dir.exists())// if directory is not here
        Dir.mkdirs() // make directory
}

Do you have declared permission in Manifest.xml file?

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

Also I suggest you to not going for HardCoded path of /mnt/extsd/ Instead of it just use Environment.getExternalStorageDirectory().getPath() .

final String PATH = Environment.getExternalStorageDirectory() + "/myfolder/";

if(!(new File(PATH)).exists()) 
new File(PATH).mkdirs();

include permission in manifest::

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

Add permission in Manifest file

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

  File sdDir = Environment.getExternalStorageDirectory();

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