检查设备上是否插入了可拆卸的SD卡。

4

我正在开发一款特定设备的应用程序。当我获取外部存储路径时,它会给出一个位置,该位置位于内部存储器上。我需要找出设备上是否有可移动的SD卡(true,false)。


1
可能是重复的问题:如何获取Android 4.0+的外部SD卡路径? - Robert
你运气不好。Android没有提供这种方法。你应该让你的应用程序用户指示它。 - greenapps
2个回答

0
请使用以下代码检查外部SD卡是否存在。
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
 if(isSDPresent){

}else{

}

这并没有真正回答所问的问题。 - Chris Stratton
你想要检查外部SD卡是否存在,对吗? - Fahim
这将返回主要外部存储的状态。发帖者的陈述意味着他们正在测试的设备上的主要外部存储路径指向内置存储器,而不是可移动卡。 - Chris Stratton
不,这不是我要找的。我需要找到关于可移动SD卡的信息。有时外部存储可以成为设备的内部存储器。 - Jenya Kirmiza

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接