问题
In my splash screen, I want to check if the phone has a SDCard. The Boolean statement is beneath:
Boolean isSDPresent = android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED );
So, if I have the SDCard in the slot on my phone, this boolean will return true, so far so good. When I go to the \"Unmount SDCard\" from the settings menu, and removes the SDCard, then kill the app and launching it again, the boolean will also be true..
And if I launches the Astro File Manager
after unmounting and removing the sdcard, I can still access the /mnt/sdcard
path, why?
How can I manage to accomplish this?
Thanks in advance!
EDIT
Testing with the following code:
File path = Environment.getExternalStorageDirectory();
String pathS = path.getPath();
When the SDCard is in the slot, the pathS
contains mnt/sdcard
, but when I removes the SDCard the pathS
is still /mnt/sdcard
...
回答1:
I've found that phones, like the Galaxy phones from Samsung, have /mnt/sdcard
point to internal memory and not the external SD card as expected.
You can know if the path returned by Environment.getExternalStorageDirectory() is actually the external SD card with a call to Environment.isExternalStorageRemovable()
Just wanted to add from the docs for getExternalStorageDirectory() this important note:
Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.
回答2:
Shouldn't it be:
boolean isPresent = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED
);
As the documentation states for Environment.getExternalStorageState():
Gets the current state of the primary "external" storage device.
And for the Environment.MEDIA_MOUNTED-constant:
getExternalStorageState()
returnsMEDIA_MOUNTED
if the media is present and mounted at its mount point with read/write access.
And this will work from API Level 1+
回答3:
I modificated that if sd card exists, sets the path there if not sets it at 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:
If you want to show whether sdcard is inserted or not then just copy-paste this code its working 100% for all Devices, code is here:
File file = new File("/mnt/extSdCard");
try
{
File list[] = file.listFiles();
Toast.makeText(getApplicationContext(), "Yes sdcard is mounted, file count "+list.length, Toast.LENGTH_LONG).show();
}
catch(NullPointerException o)
{
Toast.makeText(getApplicationContext(), "Sorry no sdcard is mounted:", Toast.LENGTH_LONG).show();
}
回答5:
We have two memory status. One is Internal memory card and other is external sd card. It depends upon device manufactures how they have manufactured the memory card path. So if you are checking for sd card availability then it may return true in both cases due to internal memory. So go for this method:
File[] listOfInternalAndExternalStorage=context.getExternalFilesDirs(null);
if(listOfInternalAndExternalStorage.length>=2){
// it contains sd card
}
above code will check if listOfInternalAndExternalStorage is more than one then it has external storage, otherwise it does not contain sd card.
回答6:
//Try this if you prefer to save external...
public String preferextstorage(Context con){
int version = Build.VERSION.SDK_INT;
String result=con.getFilesDir().getPath();
if (version>=19) {
File[] listOfInternalAndExternalStorage = con.getExternalFilesDirs(null);
if (listOfInternalAndExternalStorage.length >= 2) {
// it contains sd card
if (listOfInternalAndExternalStorage[1]==null){
result=listOfInternalAndExternalStorage[0].getPath();
}else
{
result=listOfInternalAndExternalStorage[1].getPath();
}
return result;
}
}
return result;
}
回答7:
This code snippet might be of use for you to detect and deal with the exception of Samsung devices:
public static String getExternalStorage() {
String str = Environment.getExternalStorageDirectory().getAbsolutePath();
if ( isSamsungDevice() ) {
if ( isSamsungExternalSDMounted() ) {
str += "/external_sd";
}
}
return str;
}
private static boolean isSamsungDevice() {
return new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/external_sd" ).exists();
}
private static boolean isSamsungExternalSDMounted() {
return exec( "mount" ).indexOf( "external_sd" ) >= 0;
}
public static String exec( String paramString ) {
try {
String str = "";
InputStream localInputStream = Runtime.getRuntime().exec( paramString ).getInputStream();
byte[] arrayOfByte = new byte[1024];
StringBuilder localStringBuilder = new StringBuilder();
while ( true ) {
int i = localInputStream.read( arrayOfByte, 0, arrayOfByte.length );
if ( i == -1 ) {
str = localStringBuilder.toString();
break;
}
localStringBuilder.append( new String( arrayOfByte, 0, i ) );
}
return str;
} catch ( IOException e ) {
e.printStackTrace();
return null;
}
}
来源:https://stackoverflow.com/questions/12087510/check-if-the-sdcard-is-present-boolean-is-always-true