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

人走茶凉 提交于 2019-12-03 07:46:06

问题


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 .


回答1:


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


来源:https://stackoverflow.com/questions/16688989/how-to-differentiate-between-internal-and-external-sd-card-path-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!