-2

Below is the code written for retrieving the sd card directory. I have been using execution of command and changed into reading the /proc/mounts. My Question is whether it is the right code ?

Not an expert on Linux OS. Will the /proc/mounts path be same for all the devices ? I think this code will be also free of any command injection possiblities.

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

cheers, Saurav

saurav
  • 5,388
  • 10
  • 56
  • 101
  • y so much to do to get android sdcard path – Shakeeb Ayaz Feb 18 '14 at 12:22
  • Environment.getExternalStorageDirectory(); – Shakeeb Ayaz Feb 18 '14 at 12:23
  • As far as i know using this API will not be correct since different manufcatures will consider the external storage differently.Check this http://stackoverflow.com/questions/9340332/how-can-i-get-the-list-of-mounted-external-storage-of-android-device/19982338#19982338 – saurav Feb 18 '14 at 13:14

2 Answers2

4

On Android you can get the sd card's directory on any device through

Environment.getExternalStorageDirectory();

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

LuigiPower
  • 1,113
  • 10
  • 20
  • As far as i know using this API will not be correct since different manufcatures will consider the external storage differently.Check this http://stackoverflow.com/questions/9340332/how-can-i-get-the-list-of-mounted-external-storage-of-android-device/19982338#19982338 – saurav Feb 18 '14 at 13:03
  • Actually it's the api that is meant to specifically avoid that problem – LuigiPower Feb 18 '14 at 13:04
  • Also check here http://stackoverflow.com/questions/11281010/how-can-i-get-external-sd-card-path-for-android-4-0. Envrionment.getExternalStorageDirectory() does not work in all the situations – saurav Feb 18 '14 at 13:32
  • It should still give a directory you can write into... I always use it and never had a problem. Well, ignore my answer I guess. – LuigiPower Feb 18 '14 at 13:36
2

Please use

Environment.getExternalStorageDirectory(); to get the path to SD card.

Also, use Environment.getExternalStorageState() against attribute Environment.MEDIA_MOUNTED etc, to check if the SD card is Readable, Mounted etc. :)

giorashc
  • 13,691
  • 3
  • 35
  • 71
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
  • As far as i know using this API will not be correct since different manufcatures will consider the external storage differently.Check this http://stackoverflow.com/questions/9340332/how-can-i-get-the-list-of-mounted-external-storage-of-android-device/19982338#19982338 – saurav Feb 18 '14 at 13:03
  • I am really not sure about that. However google docs says that this is the method to access it. http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29 – Atul O Holic Feb 18 '14 at 13:13
  • Also check here http://stackoverflow.com/questions/11281010/how-can-i-get-external-sd-card-path-for-android-4-0. Envrionment.getExternalStorageDirectory() does not work in all the situations – saurav Feb 18 '14 at 13:33