在Android设备中获取SD卡目录

时间:2014-02-18 12:19:56

标签: android linux

以下是为检索sd卡目录而编写的代码。 我一直在使用命令执行并更改为读取/ proc / mounts。 我的问题是它是否是正确的代码?

不是Linux操作系统专家。所有设备的/ proc / mounts路径是否相同? 我认为这段代码也没有任何命令注入可能性。

// Process process = new ProcessBuilder().command("mount").start();
        // process.waitFor();

        reader = new BufferedReader(new FileReader("/proc/mounts"));
        String line;
        while ((line = reader.readLine()) != null) {
            // Output the line of output from the mount command
            logger.debug("   {}", line);

            if (line.startsWith("/dev/block/vold/")) {
                String[] tokens = line.split(" ");
                if (tokens.length >= 3 && (tokens[2].equals("vfat") || tokens[2].equals("exfat"))) {
                    String path = tokens[1];
                    File file = new File(path);
                    if (file.exists() && file.isDirectory()) {
                        logger.debug("Detected SD card at {}", file.getPath());

                        if (!file.canWrite()) {
                            logger.warn("The SD card path {} is reporting that it is not writable", file.getPath());
                        }

                        return path;
                    }
                }
            }
        }

欢呼声, Saurav

2 个答案:

答案 0 :(得分:4)

在Android上,您可以通过

在任何设备上获取SD卡的目录
Environment.getExternalStorageDirectory();

http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()

答案 1 :(得分:2)

请使用

Environment.getExternalStorageDirectory();获取SD卡的路径。

另外,对属性Environment.getExternalStorageState()等使用Environment.MEDIA_MOUNTED来检查SD卡是否可读,已安装等。:)

相关问题