如何在Android的内部和外部的SD卡路径区分 [英] how to differentiate between internal and external SD card path in android

查看:175
本文介绍了如何在Android的内部和外部的SD卡路径区分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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
}             

要实现这一目标,我们使用了code为:

To achieve this we used the code as:

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

以上code未能在一些最新的Andr​​oid手机。请帮助我们这一点。

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

下面是我们所尝试的细节:

Below are the details of what we have tried:

  • 在具有ICS和软糖的操作系统Android的手机,可移动SD卡的确切路径有所不同,因为设备制造商。

请参阅下面

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

在这些设备中我们取得了上述code:

When running the above code in these devices we got:

  • Android的API Environment.getExternalStorageDirectory()。getPath()只返回内部SD路径

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

另外,该API Environment.getExternalStorageState()中,即使SD卡被移除上述装置返回Environment.MEDIA_MOUNTED。

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

进一步的API Environment.isExternalStorageRemovable()可移动SD卡是否为present与否总是返回false以上的设备。

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

我们搜查谷歌和尝试了一些功能,它们通经给出了被安装在device.But存储路径它不列入指示removale SD卡是否为present与否只列出。

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 .

推荐答案

要得到内部SD卡

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

要获得外部SD卡

String externalStorage = System.getenv("SECONDARY_STORAGE");
File f_secs = new File(externalStorage );

这篇关于如何在Android的内部和外部的SD卡路径区分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆