Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android设备中获取sd卡目录_Android_Linux - Fatal编程技术网

在Android设备中获取sd卡目录

在Android设备中获取sd卡目录,android,linux,Android,Linux,下面是为检索sd卡目录而编写的代码。 我一直在使用命令的执行,并改为读取/proc/mounts。 我的问题是它是否是正确的代码 不是Linux操作系统专家。所有设备的/proc/mounts路径是否相同? 我认为这段代码也不会有任何命令注入的可能性 // Process process = new ProcessBuilder().command("mount").start(); // process.waitFor(); reader = new Buff

下面是为检索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

在Android上,您可以通过

Environment.getExternalStorageDirectory();
请使用


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


另外,对属性
Environment.MEDIA\u MOUNTED
etc使用
Environment.getExternalStorageState()
,检查SD卡是否可读、是否已挂载等:)

y要获得android SD卡路径环境,需要做很多工作。getExternalStorageDirectory();据我所知,使用这个API将是不正确的,因为不同的手工操作将不同地考虑外部存储。据我所知,使用这个API来检查这一点将是不正确的,因为不同的手工操作将不同地考虑外部存储器。问题也请检查这里。getExternalStorageDirectory()在所有情况下都不起作用,它仍应提供一个可写入的目录。。。我总是用它,从来没有问题。嗯,不要理会我的答案。据我所知,使用这个API将是不正确的,因为不同的手工操作会不同地考虑外部存储器。然而谷歌文档说这是访问它的方法。也在这里查一下。getExternalStorageDirectory()在所有情况下都不起作用