how to check internal and external storage if exist
How do i know开发者_如何转开发 if there are internal and external storage in android pragmatically? does anyone how to know how to check both internal and external storage
thanks in advance
it's already explained in android documentation.
Code taken from documentation
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
I wrote a little class for that checking the storage state. Maybe it's of some use for you.
UPDATE: Cleaned up code, removed comments and made class static.
import android.os.Environment;
public class StorageHelper {
private static boolean externalStorageReadable, externalStorageWritable;
public static boolean isExternalStorageReadable() {
checkStorage();
return externalStorageReadable;
}
public static boolean isExternalStorageWritable() {
checkStorage();
return externalStorageWritable;
}
public static boolean isExternalStorageReadableAndWritable() {
checkStorage();
return externalStorageReadable && externalStorageWritable;
}
private static void checkStorage() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
externalStorageReadable = externalStorageWritable = true;
} else if (state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
externalStorageReadable = true;
externalStorageWritable = false;
} else {
externalStorageReadable = externalStorageWritable = false;
}
}
}
Code from the documentation that's been simplified a bit since previous answers:
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
I got it working if someone is searching for this.... it will help :D
try {
File dir = new File("/mnt/");
File[] dirs = dir.listFiles();
for (File _tempDIR : dirs) {
String sdCard = _tempDIR.getAbsolutePath().toString();
File file = new File(sdCard + "/"
+ Environment.DIRECTORY_DOWNLOADS);
File[] files = file.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
String _temp = files[i].getAbsoluteFile().getName()
.toString();/*Your code, and what you want to find, from all the Sdcard, internal and external. Everything mounted will be found :D*/
File f = new File("/mnt/sdcard/ext_sd");
if (f.exists()) {
// Do Whatever you want sdcard exists
}
else{
Toast.makeText(MainActivity.this, "Sdcard not Exists", Toast.LENGTH_SHORT).show();
}
精彩评论