获取内置和外置存储路径

发现对手机存储卡名称更改时,适用6.0以下的,6.0手机sd卡挂载和内置SD卡路径获取出来和6.0一下有区别!

true为获取sd卡路径,false为获取内置路径! 

目前对6.0,5.1,5.0,4.4兼容,具体代码见:

http://blog.csdn.net/lihuan6656/article/details/52356376

以下时获取SD卡路径:

String currentDir;
if(Util.getExternalStorage()==null||Util.getPath().equals("/mnt/shell/emulated/0")){
//内置SD卡路径
//Util.getPath().equals("/mnt/shell/emulated/0")  这个是平板的内置路径判断
    currentDir = Environment.getExternalStorageDirectory().getAbsolutePath();
}else {
//外置SD卡路径
    currentDir = Util.getExternalStorage();
}
 
 
 
//获取外置SD卡
//@return 有外置sd卡返回SD卡路径,无返回空
static String getExternalStorage(){
    String exts =  Environment.getExternalStorageDirectory().getPath();
    try {
        FileReader fr = new FileReader(new File("/proc/mounts"));
        BufferedReader br = new BufferedReader(fr);
        String sdCard=null;
        String line;
        while((line = br.readLine())!=null){
            if(line.contains("secure") || line.contains("asec")) continue;
            if(line.contains("fat")){
                String[] pars = line.split("\\s");
                if(pars.length<2) continue;
                if(pars[1].equals(exts)) continue;
                sdCard =pars[1];
                break;
            }
        }
        fr.close();
        br.close();
        return sdCard;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
//@return 有SD卡返回SD卡路径,无SD卡返回内置路径
public static String getPath() {
    String sdcard_path = null;
    String sd_default = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
    if (sd_default.endsWith("/")) {
        sd_default = sd_default.substring(0, sd_default.length() - 1);
    }
    // 得到路径
    try {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("mount");
        InputStream is = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        String line;
        BufferedReader br = new BufferedReader(isr);
        while ((line = br.readLine()) != null) {
            if (line.contains("secure"))
                continue;
            if (line.contains("asec"))
                continue;
            if (line.contains("fat") && line.contains("/mnt/")) {
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                    if (sd_default.trim().equals(columns[1].trim())) {
                        continue;
                    }
                    sdcard_path = columns[1];
                }
            } else if (line.contains("fuse") && line.contains("/mnt/")) {
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                    if (sd_default.trim().equals(columns[1].trim())) {
                        continue;
                    }
                    sdcard_path = columns[1];
                }
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sdcard_path;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值