android获取内置和外置SD卡路径

本文将介绍android真机环境下如何获取内置和外置SD卡路径。


测试环境:三星Note3,其他手机待测试。。。


所需权限(AndroidManifest.xml文件里)

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

获取路径代码(MainActivity.java文件)

[java]  view plain copy
  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 plain copy
  1. 内置SD卡路径:/storage/emulated/0  
  2. 外置SD卡路径:/storage/extSdCard  

2. 取出外置SD卡后

[plain]  view plain copy
  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>  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值