Praveen
Praveen

Reputation: 231

how to differentiate between internal and external SD card path in android

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:

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:

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 .

Upvotes: 8

Views: 13067

Answers (1)

Vaishali Sutariya
Vaishali Sutariya

Reputation: 5121

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

Upvotes: 2

Related Questions