检查设备上是否有可移动的SD卡

时间:2015-02-27 16:25:07

标签: android

我正在为特定设备开发应用。当我获得外部存储路径时,它会为我提供位于内部存储器上的位置。我需要找出设备上是否有可移动的SD卡(true,false)。

2 个答案:

答案 0 :(得分:0)

使用此代码检查是否存在外部SD卡

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
 if(isSDPresent){

}else{

}

答案 1 :(得分:0)

它对我有用

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;
}