6

我正在寻找一种方法来检测和访问各种 Android 设备(三星、摩托罗拉、LG、索尼、HTC)上的可移动 sd 卡。

我还需要与 2.2 兼容,所以Environment.isExternalStorageRemovable()对我来说不可用。

摩托罗拉有自己的图书馆,对于三星我可以检测到/external_sd/

我对其他人一无所知。例如,我/_ExternalSD/在一些 LG 上看到过,但即使删除了 SD,该目录仍然存在。

一个额外的问题:是否会ACTION_MEDIA_MOUNTED为他们中的任何一个广播意图

对此的任何提示都会非常有帮助。

4

5 回答 5

4

这是我用来查找设备上所有 SD 卡的类;内置和可拆卸。我一直在冰淇淋三明治上使用它,但它应该在 2 倍的水平上工作。

public class GetRemovableDevice {

private final static String TAG = "GetRemoveableDevice";

public GetRemovableDevice() {
}

public static String[] getDirectories() {
    MyLog.d(TAG, "getStorageDirectories");
    File tempFile;
    String[] directories = null;
    String[] splits;
    ArrayList<String> arrayList = new ArrayList<String>();
    BufferedReader bufferedReader = null;
    String lineRead;

    try {
        arrayList.clear(); // redundant, but what the hey
        bufferedReader = new BufferedReader(new FileReader("/proc/mounts"));

        while ((lineRead = bufferedReader.readLine()) != null) {
            MyLog.d(TAG, "lineRead: " + lineRead);
            splits = lineRead.split(" ");

            // System external storage
            if (splits[1].equals(Environment.getExternalStorageDirectory()
                    .getPath())) {
                arrayList.add(splits[1]);
                MyLog.d(TAG, "gesd split 1: " + splits[1]);
                continue;
            }

            // skip if not external storage device
            if (!splits[0].contains("/dev/block/")) {
                continue;
            }

            // skip if mtdblock device

            if (splits[0].contains("/dev/block/mtdblock")) {
                continue;
            }

            // skip if not in /mnt node

            if (!splits[1].contains("/mnt")) {
                continue;
            }

            // skip these names

            if (splits[1].contains("/secure")) {
                continue;
            }

            if (splits[1].contains("/mnt/asec")) {
                continue;
            }

            // Eliminate if not a directory or fully accessible
            tempFile = new File(splits[1]);
            if (!tempFile.exists()) {
                continue;
            }
            if (!tempFile.isDirectory()) {
                continue;
            }
            if (!tempFile.canRead()) {
                continue;
            }
            if (!tempFile.canWrite()) {
                continue;
            }

            // Met all the criteria, assume sdcard
            arrayList.add(splits[1]);
        }

    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
            }
        }
    }

    // Send list back to caller

    if (arrayList.size() == 0) {
        arrayList.add("sdcard not found");
    }
    directories = new String[arrayList.size()];
    for (int i = 0; i < arrayList.size(); i++) {
        directories[i] = arrayList.get(i);
    }
    return directories;
}

}

MyLog.d 是一个扩展 Log.d 的跟踪类 - 它可以被删除。

该类读取 /proc/mounts/ 并且:

  1. 检查路径名是否为内部 sdcard 目录
  2. 检查它是否是块设备
  3. 跳过 mtdblock 设备
  4. 跳过未安装的任何内容
  5. 跳过安全目录和 asec 目录
  6. 确保它存在,是一个目录,并且可以读/写

如果所有这些都匹配,则假定您有一个 sdcard 并将路径添加到数组列表中。它返回一个路径名的字符串数组。

要调用 getDirectories 函数,请编写类似于以下内容的代码:

String[] sdcardDirectories = GetRemoveableDevice.getDirectories();

返回的路径可用于创建用户选择列表、扫描文件等。

最后,这是来自模拟器测试的两行 MyLog.d(第二行是模拟器 sdcard):

09-19 15:57:12.511: D/GetRemoveableDevice(651): lineRead: /dev/block/mtdblock2 /cache yaffs2 rw,nosuid,nodev 0 0

09-19 15:57:12.511: D/GetRemoveableDevice(651): lineRead: /dev/block/vold/179:0 /mnt/sdcard vfat rw,dirsync,nosuid,nodev,noexec,uid=1000,gid=1015 ,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0

于 2012-09-19T20:02:29.577 回答
2

基于 Howards 课程,我做了一些修改以使其在 Galaxy S3 上工作。

  1. Environment.getExternalStorageDirectory() 返回 S3 上的内部存储。
  2. 可移动存储不一定安装在 /mnt 下
  3. 可移动媒体必须有 vfat 文件系统

_

public static String getDirectory() {
        Log.d(TAG, "getStorageDirectories");
        File tempFile;
        String[] splits;
        ArrayList<String> arrayList = new ArrayList<String>();
        BufferedReader bufferedReader = null;
        String lineRead;

        try {
            arrayList.clear(); // redundant, but what the hey
            bufferedReader = new BufferedReader(new FileReader("/proc/mounts"));

            while ((lineRead = bufferedReader.readLine()) != null) {
                Log.d(TAG, "lineRead: " + lineRead);
                splits = lineRead.split(" ");

                // skip if not external storage device
                if (!splits[0].contains("/dev/block/")) {
                    continue;
                }

                // skip if mtdblock device
                if (splits[0].contains("/dev/block/mtdblock")) {
                    continue;
                }

                // skip if not in vfat node
                if (!splits[2].contains("vfat")) {
                    continue;
                }

                // skip these names
                if (splits[1].contains("/secure")) {
                    continue;
                }

                if (splits[1].contains("/mnt/asec")) {
                    continue;
                }

                // Eliminate if not a directory or fully accessible
                tempFile = new File(splits[1]);
                if (!tempFile.exists()) {
                    continue;
                }
                if (!tempFile.isDirectory()) {
                    continue;
                }
                if (!tempFile.canRead()) {
                    continue;
                }
                if (!tempFile.canWrite()) {
                    continue;
                }

                // Met all the criteria, assume sdcard
                return splits[1];
            }

        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                }
            }
        }

        return null;
    }
于 2013-11-30T16:59:56.050 回答
2

基于 Howards 课程,我进一步修改了课程,注意到我能找到的几部手机和平板电脑上的所有外部可移动存储都是使用卷安装守护程序vold 安装的。

            // skip if not external storage device
            if (!splits[0].contains("vold")) {
                continue;
            }

            if (splits[1].contains("/mnt/asec")) {
                continue;
            }

            // Eliminate if not a directory or fully accessible
            tempFile = new File(splits[1]);
            if (!tempFile.exists()) {
                continue;
            }
            if (!tempFile.isDirectory()) {
                continue;
            }
            if (!tempFile.canRead()) {
                continue;
            }
            if (!tempFile.canWrite()) {
                continue;
            }

            // Met all the criteria, assume sdcard
            arrayList.add(splits[1]);
于 2014-09-04T05:05:38.493 回答
1

这些功能适用于所有 Android 版本:

  • 要获取外部存储上的应用程序文件夹,请调用Context.getExternalFilesDir.

  • 请记住,您的应用需要明确的权限才能访问外部存储,并且您应该通过以下方式检查它是否可用Environment.getExternalStorageState

  • 是的,ACTION_MEDIA_MOUNTED只要可移动媒体变得可访问,就会广播(您还应该收听ACTION_MEDIA_EJECTACTION_MEDIA_REMOVED

于 2012-04-23T14:27:42.063 回答
1

这是我创建并正在使用的方法。这适用于三星 Galaxy S4、三星 Galaxy Note 3 和索尼 Xperia Z2。

private static String[] getRemovableStoragePaths() {
    String[] directories;
    String[] splits;
    ArrayList<String> pathList = new ArrayList<String>();
    BufferedReader bufferedReader = null;
    String lineRead;

    try {
        bufferedReader = new BufferedReader(new FileReader("/proc/mounts"));

        while ((lineRead = bufferedReader.readLine()) != null) {
            Log.d(TAG, "lineRead: " + lineRead);
            splits = lineRead.split(" ");
            Log.d(TAG, "Testing path: " + splits[1]);

            if (!splits[1].contains("/storage")) {
                continue;
            }

            if (splits[1].contains("/emulated")) {
                // emulated indicates an internal storage location, so skip it.
                continue;
            }

            // Eliminate if not a directory or fully accessible
            Log.d(TAG, "Path found: " + splits[1]);

            // Met all the criteria, assume sdcard
            pathList.add(splits[1]);
        }

    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
            }
        }
    }

    // Send list back to caller

    if (pathList.size() == 0) {
        pathList.add("sdcard not found");
    } else {
        Log.d(TAG, "Found potential removable storage locations: " + pathList);
    }
    directories = new String[pathList.size()];
    for (int i = 0; i < pathList.size(); i++) {
        directories[i] = pathList.get(i);
    }
    return directories;
}
于 2014-09-06T22:06:40.540 回答