4

I am having a problem. I have a galaxy tab that comes with 16gb of internal storage and 2 gb of sd-card card.

when I use environment.getExternalStorageState, it returns mounted. However, when I remove the sd-card from my galaxy tab, it still returns mounted because it considers the internal storage as the external storage. Is there any way in which I can differentiate between the actual SD-Card and the Internal Storage?

Thanks

Farhan
  • 3,206
  • 14
  • 49
  • 62

4 Answers4

2

There doesn't seem to be any public API for this currently. You could check by looking at /proc/mounts but your code would then be device-dependent, since not all devices mount secondary external storage (SD card) at the same place.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • there must be a solution because when I go to my device's settings and then sd-card options, it can certainly distinguish between the actual sd-card and internal storage. so, it means that there must be a way of finding it.. any help? – Farhan Aug 16 '11 at 04:12
  • I didn't say there is no solution, just there is no public API for this. As I said, parse `/proc/mounts` to find out what devices are currently mounted. – Nikolay Elenkov Aug 16 '11 at 04:38
  • Like a regular text file. Use `cat` in a shell to see what's in it. – Nikolay Elenkov Aug 16 '11 at 10:19
  • can you tell me how to programmatically read it.. any examples plz? – Farhan Aug 16 '11 at 10:26
0

I think /storage/sdcard0/ is used for internal sdcard and /storage/sdcard1/ is used for external sd card if there are two storage option present. if you are checking a file is present either in any of sdcard or not you should check all possible paths.

String path;    
if(new File("/mnt/sdcard/yourpath").exists()) {
    path="/storage/sdcard/yourpath";
} else if(new File("/storage/sdcard0/yourpath").exists()) {
    path="/storage/sdcard0/yourpath";
} else if(new File("/storage/sdcard1/yourpath").exists()) {
    path="/storage/sdcard1/yourpath";
}
Abhishek
  • 39
  • 6
  • This is not true. Secondary storage can be mounted in many different places in file system depending on device. This is exactly the main problem with accessing it - there is no unification. The only option I can think of is to read /proc/mounts, think positive, cross your fingers and try to guess which entry is SD Card. – sdf3qrxewqrxeqwxfew3123 Oct 01 '14 at 20:07
0

I've created a check of my own, and it works. It checks if secondary, real SD card is present. Tested it on Samsung Galaxy s5 neo, Alcatel one touch 5020x, and on HTC One X. The code should work on KITKAT devices since it uses the app default dir to check.

I create a String of default app path storage on primary storage. Then change "primary" to "secondary", then try to create a folder and check for existance.

Heres the code:

String primaryStorage = Environment.getExternalStorageDirectory().getAbsolutePath();
String secondaryStorage = System.getenv("SECONDARY_STORAGE");
Boolean hasSecondary = false;

String internalSD = getExternalFilesDir(null) + "/test";
String externalSD = internalSD.replace(primaryStorage, secondaryStorage);

            try{
                File dir = new File(externalSD);
                dir.mkdirs();

                if (dir.isDirectory()) {
                    dir.delete();
                    hasSecondary = true;
                }                   
            } catch (Exception e) {
            }
Crni03
  • 61
  • 6
-2

I try like this

    if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
{

    Log.i("tag", "SDCard is mounted");

    }

or this example would help you

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148
  • 2
    In both cases (when I remove or insert the sd card), i am getting the same results.. its not working for me.. – Farhan Aug 16 '11 at 03:51