VOOZH about

URL: https://www.javacodegeeks.com/2012/10/android-finding-sd-card-path.html

⇱ Android: Finding the SD Card Path - Java Code Geeks


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?”

 File file = new File("/system/etc/vold.fstab");
 FileReader fr = null;
 BufferedReader br = null;
 
 try {
 fr = new FileReader(file);
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 } 
 
 try {
 if (fr != null) {
 br = new BufferedReader(fr);
 String s = br.readLine();
 while (s != null) {
 if (s.startsWith("dev_mount")) {
 String[] tokens = s.split("\\s");
 path = tokens[2]; //mount_point
 if (!Environment.getExternalStorageDirectory().getAbsolutePath().equals(path)) {
 break;
 }
 }
 s = br.readLine();
 }
 } 
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 try {
 if (fr != null) {
 fr.close();
 } 
 if (br != null) {
 br.close();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 }

Happy coding and don’t forget to share!

Reference: Android Tutorial: Finding the SD Card Path from our JCG partner Isaac Taylor at the Programming Mobile blog.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Isaac Taylor
Isaac Taylor
October 11th, 2012Last Updated: October 22nd, 2012
4 460 1 minute read
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Oldest
Newest Most Voted
Gopalan
11 years ago

Doesn’t work on moto-e

0
Reply
mohammad
11 years ago

doesn’t work for me too(fonpade 7)

0
Reply
Paresh Kalinani
9 years ago

I just figured out something. At least for every phone I came across today and even my Android Emulator had the SD Card Path like ‘ /storage/????-???? ‘ where every ? is a capital letter or a digit. So, if /storage/ directory has a directory which is readable and that is not the internal storage directory, it must be the SD Card. My code worked on all phones I tested and even on my android emulator! “` String removableStoragePath; File fileList[] = new File(“/storage/”).listFiles(); for (File file : fileList) { if(!file.getAbsolutePath().equalsIgnoreCase(Environment.getExternalStorageDirectory().getAbsolutePath()) && file.isDirectory() && file.canRead()) removableStoragePath = file.getAbsolutePath(); } //If… Read more »

0
Reply
mahesh ambekar
9 years ago

@Paresh Shah Kalinani
Even your solution doesn’t work

0
Reply
Back to top button
Close
wpDiscuz