【问题标题】:how to check internal and external storage if exist如果存在,如何检查内部和外部存储
【发布时间】:2011-11-28 20:09:35
【问题描述】:

我怎么知道android中是否有内部和外部存储?有谁知道如何检查内部和外部存储

提前致谢

【问题讨论】:

标签: android storage


【解决方案1】:

android documentation已经解释过了。

取自文档的代码

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;
}

【讨论】:

    【解决方案2】:

    如果有人在搜索这个,我会得到它的工作......它会有所帮助:D

    try {
                File dir = new File("/mnt/");
                File[] dirs = dir.listFiles();
                for (File _tempDIR : dirs) {
                    String sdCard = _tempDIR.getAbsolutePath().toString();
    
                    File file = new File(sdCard + "/"
                            + Environment.DIRECTORY_DOWNLOADS);
                    File[] files = file.listFiles();
                    if (files != null) {
                        for (int i = 0; i < files.length; i++) {
                            String _temp = files[i].getAbsoluteFile().getName()
                                    .toString();/*Your code, and what you want to find, from all the Sdcard, internal and external. Everything mounted will be found :D*/
    

    【讨论】:

      【解决方案3】:

      我写了一个小类来检查存储状态。或许对你有用。

      更新: 清理了代码,删除了 cmets 并将类设为静态。

      import android.os.Environment;
      
      public class StorageHelper {
      
          private static boolean externalStorageReadable, externalStorageWritable;
      
          public static boolean isExternalStorageReadable() {
              checkStorage();
              return externalStorageReadable;
          }
      
          public static boolean isExternalStorageWritable() {
              checkStorage();
              return externalStorageWritable;
          }
      
          public static boolean isExternalStorageReadableAndWritable() {
              checkStorage();
              return externalStorageReadable && externalStorageWritable;
          }
      
          private static void checkStorage() {
              String state = Environment.getExternalStorageState();
              if (state.equals(Environment.MEDIA_MOUNTED)) {
                  externalStorageReadable = externalStorageWritable = true;
              } else if (state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
                  externalStorageReadable = true;
                  externalStorageWritable = false;
              } else {
                  externalStorageReadable = externalStorageWritable = false;
              }
          }
      
      }
      

      【讨论】:

      • 有点离题,但我将如何使用这个类?没有构造函数!
      • @jesses.co.tt 就像这样:StorageHelper helper = new StorageHelper();
      • 嗯......这就是我尝试过的,但它给了我错误......我最终只是编写了一个默认构造函数,这很好......也许在我的编译设置中?无论如何,谢谢!
      • 对于代码片段 if (state.equals(Environment.MEDIA_MOUNTED)) {...} else if (state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {..else if (state.equals(Environment.MEDIA_MOUNTED) ||.. 有什么作用?
      • else if (state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY) 应该是else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)
      【解决方案4】:
      File f = new File("/mnt/sdcard/ext_sd");
          if (f.exists()) {
              // Do Whatever you want sdcard exists
          }
          else{
              Toast.makeText(MainActivity.this, "Sdcard not Exists", Toast.LENGTH_SHORT).show();
          }
      

      【讨论】:

      • 你不应该硬编码任何外部 sd 卡路径,因为没有关于路径的标准。每个供应商都可以使用自己的路径标准。它有很多变体,比如三星galaxy家族使用的"/mnt/sdcard/external_sd",LG使用的"/storage/external_SD",HTC One Max使用的"/storage/ext_sd"等。请改用Environment.getExternalStorageDirectory().getPath()
      【解决方案5】:

      the documentation 中的代码自之前的答案以来已经简化了一点:

      /* Checks if external storage is available for read and write */
      public boolean isExternalStorageWritable() {
          String state = Environment.getExternalStorageState();
          if (Environment.MEDIA_MOUNTED.equals(state)) {
              return true;
          }
          return false;
      }
      
      /* Checks if external storage is available to at least read */
      public boolean isExternalStorageReadable() {
          String state = Environment.getExternalStorageState();
          if (Environment.MEDIA_MOUNTED.equals(state) ||
              Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
              return true;
          }
          return false;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多