android獲取內置和外置SD卡路徑

本文將介紹android真機環境下如何獲取內置和外置SD卡路徑。


測試環境:三星Note3,其他手機待測試。。。


所需權限(AndroidManifest.xml文件裏)

[html] view plaincopy
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

獲取路徑代碼(MainActivity.java文件)

[java] view plaincopy
  1. package com.example.androidtest;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import android.os.Bundle;  
  11. import android.os.Environment;  
  12. import android.app.Activity;  
  13. import android.view.Menu;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.   
  22.         StringBuilder log = new StringBuilder();  
  23.         String inPath = getInnerSDCardPath();  
  24.         log.append("內置SD卡路徑:" + inPath + "\r\n");  
  25.           
  26.         List<String> extPaths = getExtSDCardPath();  
  27.         for (String path : extPaths) {  
  28.             log.append("外置SD卡路徑:" + path + "\r\n");  
  29.         }  
  30.         System.out.println(log.toString());  
  31.     }  
  32.       
  33.     /** 
  34.      * 獲取內置SD卡路徑 
  35.      * @return 
  36.      */  
  37.     public String getInnerSDCardPath() {    
  38.         return Environment.getExternalStorageDirectory().getPath();    
  39.     }  
  40.   
  41.     /** 
  42.      * 獲取外置SD卡路徑 
  43.      * @return  應該就一條記錄或空 
  44.      */  
  45.     public List<String> getExtSDCardPath()  
  46.     {  
  47.         List<String> lResult = new ArrayList<String>();  
  48.         try {  
  49.             Runtime rt = Runtime.getRuntime();  
  50.             Process proc = rt.exec("mount");  
  51.             InputStream is = proc.getInputStream();  
  52.             InputStreamReader isr = new InputStreamReader(is);  
  53.             BufferedReader br = new BufferedReader(isr);  
  54.             String line;  
  55.             while ((line = br.readLine()) != null) {  
  56.                 if (line.contains("extSdCard"))  
  57.                 {  
  58.                     String [] arr = line.split(" ");  
  59.                     String path = arr[1];  
  60.                     File file = new File(path);  
  61.                     if (file.isDirectory())  
  62.                     {  
  63.                         lResult.add(path);  
  64.                     }  
  65.                 }  
  66.             }  
  67.             isr.close();  
  68.         } catch (Exception e) {  
  69.         }  
  70.         return lResult;  
  71.     }  
  72.   
  73.     @Override  
  74.     public boolean onCreateOptionsMenu(Menu menu) {  
  75.         // Inflate the menu; this adds items to the action bar if it is present.  
  76.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  77.         return true;  
  78.     }  
  79.   
  80. }  

其中,line.contains("extSdCard")判斷部分有待進一步驗證!


打印結果:

1. 插入一張外置SD卡後

[plain] view plaincopy
  1. 內置SD卡路徑:/storage/emulated/0  
  2. 外置SD卡路徑:/storage/extSdCard  

2. 取出外置SD卡後

[plain] view plaincopy
  1. 內置SD卡路徑:/storage/emulated/0  

android系統可通過Environment.getExternalStorageDirectory()獲取存儲卡的路徑,但是現在有很多手機內置有一個存儲空間,同時還支持外置sd卡插入,這樣通過Environment.getExternalStorageDirectory()方法獲取到的就是內置存儲卡的位置,需要獲取外置存儲卡的路徑就比較麻煩,這裏借鑑網上的代碼,稍作修改,在已有的手機上做了測試,效果還可以,當然也許還有其他的一些奇葩機型沒有覆蓋到。

[java] view plain copy
  1. <pre name="code" class="java">package com.example.getpath;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7.   
  8. import android.annotation.SuppressLint;  
  9. import android.app.Activity;  
  10. import android.os.Bundle;  
  11. import android.os.Environment;  
  12. import android.util.Log;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         getPath2();  
  21.     }  
  22.   
  23.     @SuppressLint("SdCardPath")  
  24.     public String getPath2() {  
  25.         String sdcard_path = null;  
  26.         String sd_default = Environment.getExternalStorageDirectory()  
  27.                 .getAbsolutePath();  
  28.         Log.d("text", sd_default);  
  29.         if (sd_default.endsWith("/")) {  
  30.             sd_default = sd_default.substring(0, sd_default.length() - 1);  
  31.         }  
  32.         // 得到路徑  
  33.         try {  
  34.             Runtime runtime = Runtime.getRuntime();  
  35.             Process proc = runtime.exec("mount");  
  36.             InputStream is = proc.getInputStream();  
  37.             InputStreamReader isr = new InputStreamReader(is);  
  38.             String line;  
  39.             BufferedReader br = new BufferedReader(isr);  
  40.             while ((line = br.readLine()) != null) {  
  41.                 if (line.contains("secure"))  
  42.                     continue;  
  43.                 if (line.contains("asec"))  
  44.                     continue;  
  45.                 if (line.contains("fat") && line.contains("/mnt/")) {  
  46.                     String columns[] = line.split(" ");  
  47.                     if (columns != null && columns.length > 1) {  
  48.                         if (sd_default.trim().equals(columns[1].trim())) {  
  49.                             continue;  
  50.                         }  
  51.                         sdcard_path = columns[1];  
  52.                     }  
  53.                 } else if (line.contains("fuse") && line.contains("/mnt/")) {  
  54.                     String columns[] = line.split(" ");  
  55.                     if (columns != null && columns.length > 1) {  
  56.                         if (sd_default.trim().equals(columns[1].trim())) {  
  57.                             continue;  
  58.                         }  
  59.                         sdcard_path = columns[1];  
  60.                     }  
  61.                 }  
  62.             }  
  63.         } catch (Exception e) {  
  64.             // TODO Auto-generated catch block  
  65.             e.printStackTrace();  
  66.         }  
  67.         Log.d("text", sdcard_path);  
  68.         return sdcard_path;  
  69.     }  
  70. }</pre><br>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章