繁体   English   中英

如何在Android应用程序的外部SD中创建目录

[英]How to create a directory in External SD in android application

我想在“ / mnt / extsd / MyFolder”此路径上创建目录。 在调用mkdir()时返回false。我在平板电脑上插入了sdcard,外部路径为“ / mnt / extsd”,并尝试在此路径上创建一个文件夹。 下面是我的代码,

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

我授予了权限。 我要在可移动SD卡的外部SD卡中创建文件夹。 我正在使用android 4.0 ICS版本的设备。

我创建了另一种方法来获取外部SD卡的路径,

  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;
  }`

通过这种方法,我得到了路径,但是在尝试创建目录时,mkdir()返回false。

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

我的三星银河s3中有两个文件夹,例如extSdCard和sdcard。

使用以下代码进行选择。

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
}

编辑

获取内部和外部存储的路径。 下面的代码适用于三星银河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);
}

现在,您可以使用os外部存储路径创建文件夹。

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
}

您已在Manifest.xml文件中声明权限吗?

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

我也建议您不要使用/mnt/extsd/ HardCoded路径,而要使用Environment.getExternalStorageDirectory().getPath()代替。

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

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

在清单中包括许可::

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

在清单文件中添加权限

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

  File sdDir = Environment.getExternalStorageDirectory();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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