2013-05-21 70 views

回答

2

使用該工具來獲取其他外部SD卡:

File storageDir= new File("/mnt/external_sd/") 

OR

File storageDir= new File("/mnt/extSdCard/") 

欲瞭解更多詳情,請參見http://mono-for-android.1047100.n5.nabble.com/detect-SD-Card-path-td5710218.html#a5710250this

所以,像這樣用一個函數來獲取列表所有分機卡...

public static HashSet<String> getExternalMounts() { 
    final HashSet<String> out = new HashSet<String>(); 
    String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*"; 
    String s = ""; 
    try { 
     final Process process = new ProcessBuilder().command("mount") 
       .redirectErrorStream(true).start(); 
     process.waitFor(); 
     final InputStream is = process.getInputStream(); 
     final byte[] buffer = new byte[1024]; 
     while (is.read(buffer) != -1) { 
      s = s + new String(buffer); 
     } 
     is.close(); 
    } catch (final Exception e) { 
     e.printStackTrace(); 
    } 

    // parse output 
    final String[] lines = s.split("\n"); 
    for (String line : lines) { 
     if (!line.toLowerCase(Locale.US).contains("asec")) { 
      if (line.matches(reg)) { 
       String[] parts = line.split(" "); 
       for (String part : parts) { 
        if (part.startsWith("/")) 
         if (!part.toLowerCase(Locale.US).contains("vold")) 
          out.add(part); 
       } 
      } 
     } 
    } 
    return out; 
} 
+0

如何通過programmaticlly獲取字符串(路徑)? – RealDream

+0

String path = storageDir.getAbsolutePath(); –

+2

硬編碼SD卡路徑不是一個好主意,它可能會在不同的設備上有所不同 – pedja

相關問題