如何建立在Android应用程序外部SD目录(How to create a directory i

2019-08-17 18:31发布

我要创建“到/ mnt / extsd / MyFolder中”这条道路上的目录。 同时呼吁的mkdir()返回false.I插在我的平板电脑的SD卡,得到了外部路径“到/ 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版本的设备。

我创建了越来越路径的FOM外置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。

Answer 1:

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

我有两个文件夹一样extSdCard和SD卡在我的三星Galaxy S3。

使用下面的代码来选择。

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
}

编辑

为了获得内部和外部存储的路径。 下面的代码工作在三星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);
}

现在,您可以使用路径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
}


Answer 2:

你已经宣布Manifest.xml文件的权限?

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

此外,我建议你不打算为硬编码路径/mnt/extsd/而不是仅仅使用Environment.getExternalStorageDirectory().getPath()



Answer 3:

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

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

包括在清单许可::

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


Answer 4:

加入许可清单文件

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

  File sdDir = Environment.getExternalStorageDirectory();


文章来源: How to create a directory in External SD in android application