检查SD卡是否可用或不可编程

我的应用程序正在为只有SD卡的手机工作。 所以编程方式,我想检查SD卡是否可用,以及如何findSD卡的可用空间。 可能吗?

如果是,我该怎么做?

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); Boolean isSDSupportedDevice = Environment.isExternalStorageRemovable(); if(isSDSupportedDevice && isSDPresent) { // yes SD-card is present } else { // Sorry } 

按照“使用外部存储”中的说明使用Environment.getExternalStorageState()

要获得外部存储空间的可用空间,请使用StatFs

 // do this only *after* you have checked external storage state: File extdir = Environment.getExternalStorageDirectory(); File stats = new StatFs(extdir.getAbsolutePath()); int availableBytes = stats.getAvailableBlocks() * stats.getBlockSize(); 

接受的答案不适用于我

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

如果设备在build立存储的情况下,它返回true; 我的解决scheme是,要检查外部文件的目录数量,如果有超过1,设备有SD卡。 它工作,我testing了几个设备

 public static void hasRealRemovableSdCard(context: Context): Boolean { return ContextCompat.getExternalFilesDirs(context, null).size() >= 2 } 

我写了一个小class来检查存储状态。 也许这对你有一些用处。

 import android.os.Environment; /** * Checks the state of the external storage of the device. * * @author kaolick */ public class StorageHelper { // Storage states private boolean externalStorageAvailable, externalStorageWriteable; /** * Checks the external storage's state and saves it in member attributes. */ private void checkStorage() { // Get the external storage's state String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { // Storage is available and writeable externalStorageAvailable = externalStorageWriteable = true; } else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) { // Storage is only readable externalStorageAvailable = true; externalStorageWriteable = false; } else { // Storage is neither readable nor writeable externalStorageAvailable = externalStorageWriteable = false; } } /** * Checks the state of the external storage. * * @return True if the external storage is available, false otherwise. */ public boolean isExternalStorageAvailable() { checkStorage(); return externalStorageAvailable; } /** * Checks the state of the external storage. * * @return True if the external storage is writeable, false otherwise. */ public boolean isExternalStorageWriteable() { checkStorage(); return externalStorageWriteable; } /** * Checks the state of the external storage. * * @return True if the external storage is available and writeable, false * otherwise. */ public boolean isExternalStorageAvailableAndWriteable() { checkStorage(); if (!externalStorageAvailable) { return false; } else if (!externalStorageWriteable) { return false; } else { return true; } } } 

我修改它,如果一个SD卡存在,它在那里设置path。 如果没有,则将其设置在内部目录中。

 Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); if(isSDPresent) { path = theAct.getExternalCacheDir().getAbsolutePath() + "/GrammarFolder"; } else { path = theAct.getFilesDir() + "/GrammarFolder"; } 
  void updateExternalStorageState() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { mExternalStorageAvailable = mExternalStorageWriteable = false; } handleExternalStorageState(mExternalStorageAvailable, mExternalStorageWriteable); } 

您可以检查外部可移动SD卡是否可用

 public static boolean externalMemoryAvailable(Activity context) { File[] storages = ContextCompat.getExternalFilesDirs(context, null); if (storages.length > 1 && storages[0] != null && storages[1] != null) return true; else return false; } 

这完美的,因为我已经testing它。

我创build了一个类来检查SD卡上的文件夹是否可用:

 public class GetFolderPath { static String folderPath; public static String getFolderPath(Context context) { if (isSdPresent() == true) { try { File sdPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/FolderName"); if(!sdPath.exists()) { sdPath.mkdirs(); folderPath = sdPath.getAbsolutePath(); } else if (sdPath.exists()) { folderPath = sdPath.getAbsolutePath(); } } catch (Exception e) { } folderPath = Environment.getExternalStorageDirectory().getPath()+"/FolderName/"; } else { try { File cacheDir=new File(context.getCacheDir(),"FolderName/"); if(!cacheDir.exists()) { cacheDir.mkdirs(); folderPath = cacheDir.getAbsolutePath(); } else if (cacheDir.exists()) { folderPath = cacheDir.getAbsolutePath(); } } catch (Exception e){ } } return folderPath; } public static boolean isSdPresent() { return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); } } 

这个简单的方法是为我工作。 在所有types的设备进行testing。

 public boolean externalMemoryAvailable() { if (Environment.isExternalStorageRemovable()) { //device support sd card. We need to check sd card availability. String state = Environment.getExternalStorageState(); return state.equals(Environment.MEDIA_MOUNTED) || state.equals( Environment.MEDIA_MOUNTED_READ_ONLY); } else { //device not support sd card. return false; } }