Check if SD card is available or not programmatically

My application works for mobile phones that only have an SD card. Therefore, I want to programmatically check whether the SD card is available or not, and how to find free space on the SD card. Is it possible?

If so, how to do it?

+45
android sd-card
Sep 15 '11 at 10:14
source share
9 answers
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 } 
+118
Sep 15 '11 at 10:16
source share

Use Environment.getExternalStorageState() , as described in "Using External Storage" .

To get available space on external storage, use 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(); 
+12
Sep 15 '11 at 10:16
source share

The accepted answer does not work for me

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

If the device has built-in storage, it returns true; My solution is that to check the number of files of external files, if there are several, the device has an SD card. It works, and I tested it for several devices.

 public static boolean hasRealRemovableSdCard(Context context) { return ContextCompat.getExternalFilesDirs(context, null).length >= 2; } 
+7
Mar 06 '17 at 10:22
source share

I wrote a small class to check the status of the repository. Maybe this is some kind of use for you.

 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 state and saves it in member attributes. */ private void checkStorage() { // Get the external storage 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; } } } 
+5
Jul 12 '13 at 9:55 on
source share

I changed it so that if an SD card exists, it sets the path there. If not, it installs it in the internal directory.

 Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); if(isSDPresent) { path = theAct.getExternalCacheDir().getAbsolutePath() + "/GrammarFolder"; } else { path = theAct.getFilesDir() + "/GrammarFolder"; } 
+4
Sep 02 '13 at 12:28
source share
  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); } 
+3
Sep 15 2018-11-11T00:
source share

You can check if an external removable SD card is available, like this

 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; } 

This works fine as I tested it.

+2
Aug 17 '17 at 9:54 on
source share

I created a class to check if the folder on the SD card is accessible or not:

 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); } } 
0
Jun 06 '14 at 6:35
source share

This simple method works for me. Tested on all types of devices.

 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; } } 
0
Oct 27 '16 at 10:58
source share



All Articles