How to tell if the sdcard is mounted in Android?
If you're trying to access images on the device, the best method is to use the MediaStore content provider. Accessing it as a content provider will allow you to query the images that are present, and map content://
URLs to filepaths on the device where appropriate.
If you still need to access the SD card, the Camera application includes an ImageUtils class that checks if the SD card is mounted as follows:
static public boolean hasStorage(boolean requireWriteAccess) {
//TODO: After fix the bug, add "if (VERBOSE)" before logging errors.
String state = Environment.getExternalStorageState();
Log.v(TAG, "storage state is " + state);
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
Log.v(TAG, "storage writable is " + writable);
return writable;
} else {
return true;
}
} else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
Check whether the SD card is available or not programmatically
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
}
Is there a way to tell if the sdcard is mounted vs not installed at all?
for example:
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
}
Check out the possible constants at : http://developer.android.com/reference/android/os/Environment.html#getExternalStorageState%28%29
How to check if SD card inserted in Android?
This question has several duplicates, however, you can check for the SD card like this:
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
A sure shot method to find if SDCard is present
You should use getExternalFilesDirs method. This method returns path(s) to both internal memory & Micro SD card (if present). The second returned path would be path of Micro SD card. But that path may not be root directory of Micro SD card.
getExternalFilesDirs
introduced in API level 19 is the only official method that can be used to get SD card path. All others methods will return either public or private storage locations(paths) of internal memory.
I have tested this method on my phone & emulator. Kindly, note that I have passed null
as parameter for getExternalFilesDirs
during testing. But You could also pass parameter(s) like Environment.DIRECTORY_PICTURES
, Environment.DIRECTORY_MOVIES
etc.
TEST RESULTS:-
When my phone has memory card inserted, getExternalFilesDirs
returned :
1st path (internal memory): /storage/emulated/0/Android/data/my_package.appname/files
2nd path (Micro SD): /storage/sdcard1/Android/data/my_package.appname/files
Note: root directory of my SD card is: /storage/sdcard1
On my emulator, getExternalFilesDirs
returned :
1st path (internal memory): /storage/emulated/0/Android/data/my_package.appname/files
2nd path (Micro SD): /storage/11E9-4214/Android/data/my_package.appname/files
Note: On emulator, SD card is emulated(not real one).
If Micro SD card is ejected (either on phone or emulator) getExternalFilesDirs
returns null
1st path (internal memory): path to internal memory - It varies depending on device
2nd path (Micro SD): null
BUT ;
On some devices, you may not get the Micro SD card path. It means getExternalFilesDirs
will return only one path(internal memory). Second path would be empty i.e. it will not return any second path.
Reason: OEM of that particular device may not have set the path (i.e. SECONDARY_STORAGE
environment variable) in the device.
This answer for this question says,
the OEM must have set the SECONDARY_STORAGE environment variable in
the device specificinit.rc
file as mentioned here:
https://source.android.com/devices/storage/config-example.html
Also the comment made by somesh on the above mentioned answer says,
If that environment variable is not set, you can't write to the sdcard
by making use ofgetExternalFilesDirs
since its not going to return
the sdcard path.
In those kind of cases, assume that memory card is not present and save whatever you want to save in internal memory.
To Summarize, If getExternalFilesDirs
doesn't return second path or returned second path is null
, don't store in SD card (store in internal storage); else store in second path returned by getExternalFilesDirs
Suggestion:
To understand about different storage locations in Android, please go through my answer on stackoverflow
Related Topics
How to Pass a View's Onclick Event to Its Parent on Android
Creating Custom Lockscreen in Android
How to Get My Android Device Country Code Without Using Gps
Get List of Photo Galleries on Android
Error Strictmode$Androidblockguardpolicy.Onnetwork
How to Force the Action Bar to Be at the Bottom in Ics
Get Ssid When Wifi Is Connected
Android Maps API V2 with Custom Markers
Preventing Status Bar Expansion
What Will Happen to the Sharedpreferences on Update an Android App
Android: How to Get a Radiogroup with Togglebuttons
Android:Call Activity of Another Application
How Can Make My Viewpager Load Only One Page at a Time Ie Setoffscreenpagelimit(0);
Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget
Android - Setonclicklistener VS Onclicklistener VS View.Onclicklistener
How to Find MAC Address of an Android Device Programmatically
Android Keeps Caching My Intents Extras, How to Declare a Pending Intent That Keeps Fresh Extras