2015-06-22 75 views
0

在我的应用程序中,我想检查SD卡是否存在(asin mounted)。当我尝试运行该应用程序时,即使“sd卡已安装”,它仍然不存在。检查sd卡的可用性android

代码

public boolean isSDCardPresent() { 

     return android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); 

    } 

上面的代码总是返回true即使SD卡不存在。

回答

0

试着改变你的方法,下面这段代码

public boolean isSDCardPresent() { 

    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 

} 
+0

仍然得到它挂载 – androGuy

+0

你在模拟器中测试它吗? –

+0

Nah真实设备 – androGuy

0

尝试:

public boolean isSDCardPresent() { 
    return android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY); 
} 
+0

Nah无法正常工作 – androGuy

+0

在我的设备Nexus 9选项卡中。没有SD卡插槽,它返回false。 –

+0

但是,如果我把SD卡再次返回false – androGuy

0

外部(可拆卸)Sd中的路径从设备到设备的不同而不同,我也想不出一个单一的方法来检查它的可用性,所以我写了一个方法,它遍历不同制造商使用的所有不同的ext路径,然后找到完全匹配。如果找到该文件夹​​,则返回true &该卡已插入手机中。

注意:我在方法内部使用了StreamSupport库,所以您需要下载jar文件并将它添加到项目的libs文件夹,就这样,它就会工作!

public static boolean isExternalCardAvailable(Context context) { 
    List<String> listOfFoldersToSearch = Arrays.asList("/storage/", "/mnt/", "/removable/", "/data/"); 
    final List<String> listOf2DepthFolders = Arrays.asList("sdcard0", "media_rw", "removable"); 
    final List<String> listOfExtFolders = Arrays.asList("sdcard1", "extsdcard", "external_sd", "microsd", "emmc", "ext_sd", "sdext", 
      "sdext1", "sdext2", "sdext3", "sdext4"); 
    final String[] thePath = {""}; 
    Optional<File> optional = StreamSupport.stream(listOfFoldersToSearch) 
      .filter(new Predicate<String>() { 
       @Override 
       public boolean test(final String s) { 
        File folder = new File(s); 
        return folder.exists() && folder.isDirectory(); 
       } 
      }) //I got the ones that exist and are directories 
      .flatMap(new Function<String, Stream<File>>() { 
       @Override 
       public Stream<File> apply(final String s) { 
        try { 
         List<File> files = Arrays.asList(new File(s).listFiles()); 
         return StreamSupport.stream(files); 
        } catch (NullPointerException e) { 
         return StreamSupport.stream(new ArrayList<File>()); 
        } 
       } 
      }) //I got all sub-dirs of the main folders 
      .flatMap(new Function<File, Stream<File>>() { 
       @Override 
       public Stream<File> apply(final File file1) { 
        if (listOf2DepthFolders.contains(file1.getName() 
          .toLowerCase())) { 
         try { 
          List<File> files = Arrays.asList(file1.listFiles()); 
          return StreamSupport.stream(files); 
         } catch (NullPointerException e) { 
          return StreamSupport.stream(Collections.singletonList(file1)); 
         } 
        } else 
         return StreamSupport.stream(Collections.singletonList(file1)); 
       } 
      }) //Here I got all the 2 depth and 3 depth folders 
      .filter(new Predicate<File>() { 
       @Override 
       public boolean test(final File o) { 
        return listOfExtFolders.contains(o.getName() 
          .toLowerCase()); 
       } 
      }) 
      .findFirst(); 

    optional.ifPresent(new Consumer<File>() { 
     @Override 
     public void accept(final File file) { 
      thePath[0] = file.getAbsolutePath(); 
     } 
    }); 

    Log.e("Path", thePath[0]); 

    try { 
     ContextCompat.getExternalFilesDirs(context, null); 
    } catch (Exception e) { 
     Log.e("PathException", thePath[0]); 
    } 
    return !thePath[0].equals("") && new File(thePath[0]).listFiles()!=null; 
} 

P.S.我在几台HTC和三星设备上进行了测试和验证。

0

昨天我有一个非常类似的问题:

我想检查,如果在我的设备是一个物理SD卡。

的问题是,

public boolean isSDCardPresent() { 

return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);} 

总是返回真(即使SD卡已物理移除),因为仿真SD卡的。

,所以这是我的解决方案:

static boolean externalStoragecheck() { 
    return !Environment.isExternalStorageEmulated(); 
} 

有没有发现这样的事情,所以我想与大家分享。