android系統可經過Environment.getExternalStorageDirectory()獲取存儲卡的路徑,可是如今有不少手機內置有一個存儲空間,同時還支持外置sd卡插入,這樣經過Environment.getExternalStorageDirectory()方法獲取到的就是內置存儲卡的位置,須要獲取外置存儲卡的路徑就比較麻煩,這裏借鑑網上的代碼,稍做修改,在已有的手機上作了測試,效果還能夠,固然也許還有其餘的一些奇葩機型沒有覆蓋到,若是您看到這個文章後測試有問題,歡迎留言跟我交流,你們互相學習進步,關鍵代碼以下:java
//獲取外置存儲卡的根路徑,若是沒有外置存儲卡,則返回null public String getPath2() { 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; }