簡體   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