判断是否可放置外置sdcard及外置sdcard是否卸载

本文介绍了一种检测Android设备上外置SD卡状态的方法,包括如何获取外置SD卡路径、判断SD卡是否挂载及检测SD卡是否可写等关键步骤。

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

注意,判断外置sd卡是否卸载不能直接判断得到外置sd卡路径是否为空,即使外置sd卡卸载时,得到的路径,在拔出sd卡5秒内仍然可能不为空。这个问题我当时也纠结好久。

转自http://blog.csdn.net/com314159/article/details/22859059

  1. public class SdCardUtils {  
  2.   
  3.     // 返回值不带File seperater "/",如果没有外置第二个sd卡,返回null  
  4.     public static String getSecondExterPath() {  
  5.         List<String> paths = getAllExterSdcardPath();  
  6.   
  7.         if (paths.size() == 2) {  
  8.   
  9.             for (String path : paths) {  
  10.                 if (path != null && !path.equals(getFirstExterPath())) {  
  11.                     return path;  
  12.                 }  
  13.             }  
  14.   
  15.             return null;  
  16.   
  17.         } else {  
  18.             return null;  
  19.         }  
  20.     }  
  21.   
  22.     public static boolean isFirstSdcardMounted(){  
  23.         if (!Environment.getExternalStorageState().equals(  
  24.                 Environment.MEDIA_MOUNTED)) {  
  25.             return false;  
  26.         }  
  27.         return true;  
  28.     }  
  29.       
  30.     public static boolean isSecondSDcardMounted() {  
  31.         String sd2 = getSecondExterPath();  
  32.         if (sd2 == null) {  
  33.             return false;  
  34.         }  
  35.         return checkFsWritable(sd2 + File.separator);  
  36.   
  37.     }  
  38.   
  39.     // 测试外置sd卡是否卸载,不能直接判断外置sd卡是否为null,因为当外置sd卡拔出时,仍然能得到外置sd卡路径。我这种方法是按照android谷歌测试DICM的方法,  
  40.     // 创建一个文件,然后立即删除,看是否卸载外置sd卡  
  41.     // 注意这里有一个小bug,即使外置sd卡没有卸载,但是存储空间不够大,或者文件数已至最大数,此时,也不能创建新文件。此时,统一提示用户清理sd卡吧  
  42.     private static boolean checkFsWritable(String dir) {  
  43.   
  44.         if (dir == null)  
  45.             return false;  
  46.   
  47.         File directory = new File(dir);  
  48.   
  49.         if (!directory.isDirectory()) {  
  50.             if (!directory.mkdirs()) {  
  51.                 return false;  
  52.             }  
  53.         }  
  54.   
  55.         File f = new File(directory, ".keysharetestgzc");  
  56.         try {  
  57.             if (f.exists()) {  
  58.                 f.delete();  
  59.             }  
  60.             if (!f.createNewFile()) {  
  61.                 return false;  
  62.             }  
  63.             f.delete();  
  64.             return true;  
  65.   
  66.         } catch (Exception e) {  
  67.         }  
  68.         return false;  
  69.   
  70.     }  
  71.   
  72.     public static String getFirstExterPath() {  
  73.         return Environment.getExternalStorageDirectory().getPath();  
  74.     }  
  75.   
  76.     public static List<String> getAllExterSdcardPath() {  
  77.         List<String> SdList = new ArrayList<String>();  
  78.   
  79.         String firstPath = getFirstExterPath();  
  80.   
  81.         // 得到路径  
  82.         try {  
  83.             Runtime runtime = Runtime.getRuntime();  
  84.             Process proc = runtime.exec("mount");  
  85.             InputStream is = proc.getInputStream();  
  86.             InputStreamReader isr = new InputStreamReader(is);  
  87.             String line;  
  88.             BufferedReader br = new BufferedReader(isr);  
  89.             while ((line = br.readLine()) != null) {  
  90.                 // 将常见的linux分区过滤掉  
  91.                 if (line.contains("secure"))  
  92.                     continue;  
  93.                 if (line.contains("asec"))  
  94.                     continue;  
  95.                 if (line.contains("media"))  
  96.                     continue;  
  97.                 if (line.contains("system") || line.contains("cache")  
  98.                         || line.contains("sys") || line.contains("data")  
  99.                         || line.contains("tmpfs") || line.contains("shell")  
  100.                         || line.contains("root") || line.contains("acct")  
  101.                         || line.contains("proc") || line.contains("misc")  
  102.                         || line.contains("obb")) {  
  103.                     continue;  
  104.                 }  
  105.   
  106.                 if (line.contains("fat") || line.contains("fuse") || (line  
  107.                         .contains("ntfs"))) {  
  108.                       
  109.                     String columns[] = line.split(" ");  
  110.                     if (columns != null && columns.length > 1) {  
  111.                         String path = columns[1];  
  112.                         if (path!=null&&!SdList.contains(path)&&path.contains("sd"))  
  113.                             SdList.add(columns[1]);  
  114.                     }  
  115.                 }  
  116.             }  
  117.         } catch (Exception e) {  
  118.             // TODO Auto-generated catch block  
  119.             e.printStackTrace();  
  120.         }  
  121.   
  122.         if (!SdList.contains(firstPath)) {  
  123.             SdList.add(firstPath);  
  124.         }  
  125.   
  126.         return SdList;  
  127.     }  
  128. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值