41

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

BC2
  • 892
  • 1
  • 7
  • 23
oczdref
  • 534
  • 1
  • 6
  • 15

5 Answers5

48

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;
}
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
25

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

}
kaolick
  • 4,817
  • 5
  • 42
  • 53
  • Kind of off-topic, but how would I use this class? There's no Constructor ! – jesses.co.tt Apr 07 '14 at 01:24
  • @jesses.co.tt Just like this: `StorageHelper helper = new StorageHelper();` – kaolick Apr 07 '14 at 08:31
  • Hmm... that's what I tried but it was giving me errors... I ended up just writing a default constructor and it was fine ... something in my compile settings maybe? Anyways, thanks! – jesses.co.tt Apr 08 '14 at 07:23
  • I updated my code and made the class static. So no constructor necessary anymore. – kaolick Sep 10 '15 at 17:46
  • 1
    For code piece `if (state.equals(Environment.MEDIA_MOUNTED)) {...} else if (state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {` . What does the `..else if (state.equals(Environment.MEDIA_MOUNTED) ||..` do? – Weishi Z Mar 12 '16 at 04:19
  • 1
    `else if (state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)` should be `else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)` – vman Jan 18 '17 at 02:49
4

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;
}
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
  • This code is unreliable I think. For example Pixel 4A does not have an external sd card and yet this code shows true for both variables. And that too with only READ_EXTERNAL_STORAGE permission is allowed. – falero80s Jun 02 '22 at 01:31
1

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*/
Danny Fallas
  • 628
  • 1
  • 5
  • 11
0
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();
    }
Happy Singh
  • 376
  • 3
  • 8
  • 3
    You shouldn't hardcoded any external sd card path, because there is no standard about the path. Every vendor can use their own path standard. There are many variations of it, like `"/mnt/sdcard/external_sd"` used by samsung galaxy family, `"/storage/external_SD"` used by LG, `"/storage/ext_sd"` used by HTC One Max, etc. Use `Environment.getExternalStorageDirectory().getPath()` instead – HendraWD Aug 30 '17 at 09:05