8

In android application I need to do:

If(removable SdCard is present){
    String path=get the path of removable Sdcard() ;
    //using this path we will create some files in sdcard
} else{
    //display error message
}             

To achieve this we used the code as:

If (Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)){
                String path= Environment.getExternalStorageDirectory().getPath();
}

The above code fails in some of latest android mobiles. Please help us on this.

Below are the details of what we have tried:

  • In android mobiles having OS of ICS and JellyBean, the exact path of the removable sdcard varies because of device manufacturer.

See the table below

Tested Devices  OS version  removable sdcard path   internal sd path
Samsung galaxy s2   4.0.4   /mnt/sdcard/external_sd /mnt/sdcard
Samsung S Advance   4.1.2   /storage/extSdCard      /storage/sdcard0
Samsung Galaxy s3   4.0.4   /mnt/extSdCard          /mnt/sdcard
Samsung Galaxy Note 4.0.4   /mnt/sdcard/external_sd /mnt/sdcard

When running the above code in these devices we got:

  • The android API Environment.getExternalStorageDirectory().getPath() returns only the internal sd path

  • Also the the API Environment.getExternalStorageState() return Environment.MEDIA_MOUNTED in above devices even though sdcard is removed.

  • Further the API Environment.isExternalStorageRemovable() always returns false for above devices whether removable sdcard is present or not.

We searched in google and tried some functions they given.It gives only list of storage paths that are mounted to the device.But it doesnot indicate whether the removale sdcard is present or not.

The function i tried to get the storage path:

private static String getMountedPaths(){
    String sdcardPath = "";
    Runtime runtime = Runtime.getRuntime();
    Process proc = null;
    try {
        proc = runtime.exec("mount");
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    InputStream is = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    String line;
    BufferedReader br = new BufferedReader(isr);
    try {
        while ((line = br.readLine()) != null) {
            if (line.contains("secure")) continue;
            if (line.contains("asec")) continue;

            if (line.contains("fat")) {//TF card
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                    mount.add(columns[1]);
                    sdcardPath=columns[1];
                }
            } else if (line.contains("fuse")) {//internal storage
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                     mount.add(columns[1]);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sdcardPath;
}

Is there any other function better than above to get the list of storage paths in android ? also help to solve the above problem .

Pramod
  • 2,828
  • 6
  • 31
  • 40
Praveen
  • 231
  • 1
  • 4
  • 13
  • i too had similar problem i think that's the best way to get the path of internal and external device is as what you have done. And what's wrong with the above?? – Raghunandan May 22 '13 at 10:09
  • check the link here. i used a similar code as above http://stackoverflow.com/questions/16637935/a-safe-path-to-external-storage/16638064#16638064 – Raghunandan May 22 '13 at 10:17
  • "Further the API Environment.isExternalStorageRemovable() always returns false for above devices whether removable sdcard is present or not."......What do you mean ? – xmen Feb 05 '14 at 15:13

1 Answers1

2

To get internal SD card

String internalStorage = System.getenv("EXTERNAL_STORAGE");
File f_exts = new File(internalStorage );

To get external SD card

String externalStorage = System.getenv("SECONDARY_STORAGE");
File f_secs = new File(externalStorage );
rnrneverdies
  • 15,243
  • 9
  • 65
  • 95
Vaishali Sutariya
  • 5,093
  • 30
  • 32
  • or you can refer this link also http://stackoverflow.com/questions/22219312/android-open-external-storage-directorysdcard-for-storing-file this link also – Vaishali Sutariya Aug 18 '14 at 05:41