android双SD卡路径获取

本文提供了一种在Android设备上获取不同存储路径的方法,包括手机自身内存路径、内置及外置SD卡路径,并提供了检查可用空间的实用代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[java]  view plain copy
  1. /** 
  2.  * 获取手机自身内存路径 
  3.  *  
  4.  */  
  5. public static String getPhoneCardPath() {  
  6.     return Environment.getDataDirectory().getPath();  
  7.   
  8. }  
  9.   
  10. /** 
  11.  * 获取sd卡路径 双sd卡时,根据”设置“里面的数据存储位置选择,获得的是内置sd卡或外置sd卡 
  12.  *  
  13.  * @return 
  14.  */  
  15. public static String getNormalSDCardPath() {  
  16.     return Environment.getExternalStorageDirectory().getPath();  
  17. }  
  18.   
  19. /** 
  20.  * 获取sd卡路径 双sd卡时,获得的是外置sd卡 
  21.  *  
  22.  * @return 
  23.  */  
  24. public static String getSDCardPath() {  
  25.     String cmd = "cat /proc/mounts";  
  26.     Runtime run = Runtime.getRuntime();// 返回与当前 Java 应用程序相关的运行时对象  
  27.     BufferedInputStream in = null;  
  28.     BufferedReader inBr = null;  
  29.     try {  
  30.         Process p = run.exec(cmd);// 启动另一个进程来执行命令  
  31.         in = new BufferedInputStream(p.getInputStream());  
  32.         inBr = new BufferedReader(new InputStreamReader(in));  
  33.   
  34.         String lineStr;  
  35.         while ((lineStr = inBr.readLine()) != null) {  
  36.             // 获得命令执行后在控制台的输出信息  
  37.             Log.i("CommonUtil:getSDCardPath", lineStr);  
  38.             if (lineStr.contains("sdcard")  
  39.                     && lineStr.contains(".android_secure")) {  
  40.                 String[] strArray = lineStr.split(" ");  
  41.                 if (strArray != null && strArray.length >= 5) {  
  42.                     String result = strArray[1].replace("/.android_secure",  
  43.                             "");  
  44.                     return result;  
  45.                 }  
  46.             }  
  47.             // 检查命令是否执行失败。  
  48.             if (p.waitFor() != 0 && p.exitValue() == 1) {  
  49.                 // p.exitValue()==0表示正常结束,1:非正常结束  
  50.                 Log.e("CommonUtil:getSDCardPath""命令执行失败!");  
  51.             }  
  52.         }  
  53.     } catch (Exception e) {  
  54.         Log.e("CommonUtil:getSDCardPath", e.toString());  
  55.         // return Environment.getExternalStorageDirectory().getPath();  
  56.     } finally {  
  57.         try {  
  58.             if (in != null) {  
  59.                 in.close();  
  60.             }  
  61.         } catch (IOException e) {  
  62.             // TODO Auto-generated catch block  
  63.             e.printStackTrace();  
  64.         }  
  65.         try {  
  66.             if (inBr != null) {  
  67.                 inBr.close();  
  68.             }  
  69.         } catch (IOException e) {  
  70.             // TODO Auto-generated catch block  
  71.             e.printStackTrace();  
  72.         }  
  73.     }  
  74.     return Environment.getExternalStorageDirectory().getPath();  
  75. }  
  76.   
  77. // 查看所有的sd路径  
  78. public String getSDCardPathEx() {  
  79.     String mount = new String();  
  80.     try {  
  81.         Runtime runtime = Runtime.getRuntime();  
  82.         Process proc = runtime.exec("mount");  
  83.         InputStream is = proc.getInputStream();  
  84.         InputStreamReader isr = new InputStreamReader(is);  
  85.         String line;  
  86.         BufferedReader br = new BufferedReader(isr);  
  87.         while ((line = br.readLine()) != null) {  
  88.             if (line.contains("secure"))  
  89.                 continue;  
  90.             if (line.contains("asec"))  
  91.                 continue;  
  92.   
  93.             if (line.contains("fat")) {  
  94.                 String columns[] = line.split(" ");  
  95.                 if (columns != null && columns.length > 1) {  
  96.                     mount = mount.concat("*" + columns[1] + "\n");  
  97.                 }  
  98.             } else if (line.contains("fuse")) {  
  99.                 String columns[] = line.split(" ");  
  100.                 if (columns != null && columns.length > 1) {  
  101.                     mount = mount.concat(columns[1] + "\n");  
  102.                 }  
  103.             }  
  104.         }  
  105.     } catch (FileNotFoundException e) {  
  106.         // TODO Auto-generated catch block  
  107.         e.printStackTrace();  
  108.     } catch (IOException e) {  
  109.         // TODO Auto-generated catch block  
  110.         e.printStackTrace();  
  111.     }  
  112.     return mount;  
  113. }  
  114.   
  115. // 获取当前路径,可用空间  
  116. public static long getAvailableSize(String path) {  
  117.     try {  
  118.         File base = new File(path);  
  119.         StatFs stat = new StatFs(base.getPath());  
  120.         long nAvailableCount = stat.getBlockSize()  
  121.                 * ((long) stat.getAvailableBlocks());  
  122.         return nAvailableCount;  
  123.     } catch (Exception e) {  
  124.         e.printStackTrace();  
  125.     }  
  126.     return 0;  
  127. }  


自:http://blog.youkuaiyun.com/getchance/article/details/38657195

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值