Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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 .

like image 401
Praveen Avatar asked May 22 '13 10:05

Praveen


1 Answers

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 );
like image 171
Vaishali Sutariya Avatar answered Oct 13 '22 00:10

Vaishali Sutariya