【问题标题】:How to create a directory in External SD in android application如何在 Android 应用程序的外部 SD 中创建目录
【发布时间】:2013-03-09 04:12:48
【问题描述】:

我想在“/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。

【问题讨论】:

  • 不要使用硬编码文件路径使用Environment.getExternalStorageDirectory()
  • 我已经使用过 Environment.getExternalStorageDirectory() ,但是此方法返回 mnt/sdcard 路径。我需要在可移动的外部 SD 卡上创建目录。我编辑了我的问题以获取外部路径,但我需要在那里创建目录
  • 一旦获得路径,在其下创建文件夹有什么困难。

标签: android android-sdcard android-permissions


【解决方案1】:

您是否在 Manifest.xml 文件中声明了权限?

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

另外我建议你不要使用/mnt/extsd/ 的硬编码路径,而不要使用Environment.getExternalStorageDirectory().getPath()

【讨论】:

  • 感谢您的回复。我使用了这个 Environment.getExternalStorageDirectory() + "/myfolder/" 但它返回 Environment.getExternalStorageDirectory() + "/myfolder/" 这是设备 SD 卡。但我想在我的外部 sdcard 上创建我的设备版本是 4.0 ICS
【解决方案2】:
final String PATH = Environment.getExternalStorageDirectory() + "/myfolder/";

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

在清单中包含权限::

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

【讨论】:

    【解决方案3】:
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />     
    

    我的三星 Galaxy 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
    }
    

    编辑

    获取内部和外部存储的路径。以下代码适用于三星 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 external storage路径来创建文件夹了。

    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
    }
    

    【讨论】:

    • 嗨 Raghunandan,感谢您的快速回复。我授予了权限,
    • 好的。那么上面的应该工作。我使用上述方法将图像缓存在我的 sdcard 中。
    • 我尝试了你给出的代码,但它是在 sdcard 上创建的,而不是在可移动的外部 sd 卡中创建的。我已经在 mnt/sdcard 上创建了文件夹,但我想在 mnt 上创建/extsd(或者在某些设备上,外部 sd 卡路径是 mnt/extsdcard)
    • 检查你的外置SD卡安装在哪里?我三星galaxy s3。 sdcard0 是手机内部存储器,extSdCard 是外部 sd 卡。所以选择检查你可以使用上面的代码。
    • 我猜你的外部 sd 卡安装在其他地方。
    【解决方案4】:

    在 Manifest 文件中添加权限

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
      File sdDir = Environment.getExternalStorageDirectory();
    

    【讨论】:

      猜你喜欢
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 2020-12-22
      • 2019-11-18
      • 1970-01-01
      相关资源
      最近更新 更多