发现对手机存储卡名称更改时,适用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; }