Tutorial details

Android: Finding the SD Card Path | App Code for Sale | Preview

Android: Finding the SD Card Path | Android Tutorial

How to find the SD Card Path

Overview PAGE TOP

Finding the SD Card path in Android is easy right? All you have to do is use Environment.getExternalStoreDirectory(), and you’re good to go!

Well, not quite.

After all, that’s what StackOverflow says. If you actually try the above method on a Samsung device, life won’t be fun for you. Environment.getExternalStoreDirectory() actually returns the incorrect path on most Samsung devices. That’s how I came across the issue. It turns out, the above method doesn’t actually guarantee that it will return the SD Card directory. According to Android’s API documentation,

“In devices with multiple ‘external’ storage directories (such as both secure app storage and mountable shared storage), this directory represents the ‘primary’ external storage that the user will interact with.”

So the call doesn’t guarantee that the path returned truly points SD Card. There are a few other ways to get an “external” path on the device where files can be stored, though, like the getExternalFilesDir().

There are also a few other tricks to actually get the path of the SD Card directory. The below code works on most Android devices (Samsung included). It’s a pretty hacky solution, though, and who knows how long this trick will actually work (source). Instead of using the code below, it may be better to ask the question, “do I really need the SD Card directory, or just a path that I can store files to?”

01  File file = new File("/system/etc/vold.fstab");
02      FileReader fr = null;
03      BufferedReader br = null;
05      try {
06          fr = new FileReader(file);
07      } catch (FileNotFoundException e) {
08          e.printStackTrace();
09      }
11      try {
12          if (fr != null) {
13              br = new BufferedReader(fr);
14              String s = br.readLine();
15              while (s != null) {
16                  if (s.startsWith("dev_mount")) {
17                      String[] tokens = s.split("\\s");
18                      path = tokens[2]; //mount_point
19                      if (!Environment.getExternalStorageDirectory().getAbsolutePath().equals(path)) {
20                          break;
21                      }
22                  }
23                  s = br.readLine();
24              }
25          }           
26      } catch (IOException e) {
27          e.printStackTrace();
28      } finally {
29          try {
30              if (fr != null) {
31                  fr.close();
32              }           
33              if (br != null) {
34                  br.close();
35              }
36          } catch (IOException e) {
37              e.printStackTrace();
38          }
39      }

Happy coding and don’t forget to share!

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.