2015-06-22 77 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(); 
} 

有沒有發現這樣的事情,所以我想與大家分享。