如何检查Android中的外部存储中是否存在文件夹?

时间:2015-04-29 10:28:27

标签: android file-permissions android-permissions android-external-storage internal-storage

String fldr= "Main";

我需要查找目录中的任何位置是否有名为fldr的文件夹。

这将返回外部存储目录

String SDpath = Environment.getExternalStorageDirectory().toString();

如果有一个类似于fldr的文件夹,我需要获取其目录并检查其是否可写..怎么做?

1 个答案:

答案 0 :(得分:1)

这样做,

File dir = new File(Environment.getExternalStorageDirectory() + "/Main");
if(dir.exists() && dir.isDirectory()) {
    // do something here
}

/**
   * @return True if the external storage is available.
   * False otherwise.
   */
  public static boolean checkAvailable() {

    // Retrieving the external storage state
    String state = Environment.getExternalStorageState();

    // Check if available
    if (Environment.MEDIA_MOUNTED.equals(state)
        || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
      return true;
    }

    return false;
  }

  /**
   * @return True if the external storage is writable.
   * False otherwise.
   */
  public static boolean checkWritable() {

    // Retrieving the external storage state
    String state = Environment.getExternalStorageState();

    // Check if writable
    if (Environment.MEDIA_MOUNTED.equals(state)) {
      return true;
    }

    return false;

  }