Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the external SD card path for Android 4.0+?

Samsung Galaxy S3 has an external SD card slot, which is mounted to /mnt/extSdCard.

How can I get this path by something like Environment.getExternalStorageDirectory()?

This will return mnt/sdcard, and I can't find the API for the external SD card. (Or removable USB storage on some tablets.)

like image 898
Romulus Urakagi Ts'ai Avatar asked Jul 01 '12 09:07

Romulus Urakagi Ts'ai


People also ask

How do I find my SD card path on Android?

getExternalStorageDirectory() will return you the normal path for SD card which is mnt/sdcard/ . But for Samsung devices for example, the SD card path is either under mnt/extSdCard/ or under mnt/external_sd/ .

Where is the external storage folder on Android?

Environment. getExternalStorageDirectory() : return the primary external storage root directory. Context. getExternalFilesDir(String type) : return the absolute path of the directory on the primary external storage where the application can place its own files.

How do I get the SDcard path?

From this is easy to see that the way to get the SDCard path is to use a couple of environment variables: System.getenv(«EXTERNAL_STORAGE») —> Internal SDCard /STORAGE/SDCARD1. System.getenv(«EXTERNAL_SDCARD_STORAGE») —> True SDCard /STORAGE/SDCARD0.

Where is the SD card location on the device?

However, when the device features both, an internal and external SDCard, the latter usually is to be found "inside" the former, e.g. at /sdcard/external_sd. Alternatively, it may be mounted at /mnt/extSdCard or /storage/extSdCard. Note the capitals. Android filenames are case-sensitive.

How to get the SDcard path from the environment variables?

Let’s see what we can achieve with the environment variables! From this is easy to see that the way to get the SDCard path is to use a couple of environment variables: System.getenv (“EXTERNAL_STORAGE”) —> Internal SDCard /STORAGE/SDCARD1

How to transfer SD card from one phone to another?

When your user change their phone, plug the SD card to new phone, and on first phone it's /sdcard, on second phone it's /mnt/extSdCard, anything using file path is crashed. I need to generate real path from its relative path. I now this topic is old but this may help. you should use this method.


1 Answers

I have a variation on a solution I found here

public static HashSet<String> getExternalMounts() {     final HashSet<String> out = new HashSet<String>();     String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";     String s = "";     try {         final Process process = new ProcessBuilder().command("mount")                 .redirectErrorStream(true).start();         process.waitFor();         final InputStream is = process.getInputStream();         final byte[] buffer = new byte[1024];         while (is.read(buffer) != -1) {             s = s + new String(buffer);         }         is.close();     } catch (final Exception e) {         e.printStackTrace();     }      // parse output     final String[] lines = s.split("\n");     for (String line : lines) {         if (!line.toLowerCase(Locale.US).contains("asec")) {             if (line.matches(reg)) {                 String[] parts = line.split(" ");                 for (String part : parts) {                     if (part.startsWith("/"))                         if (!part.toLowerCase(Locale.US).contains("vold"))                             out.add(part);                 }             }         }     }     return out; } 

The original method was tested and worked with

  • Huawei X3 (stock)
  • Galaxy S2 (stock)
  • Galaxy S3 (stock)

I'm not certain which android version these were on when they were tested.

I've tested my modified version with

  • Moto Xoom 4.1.2 (stock)
  • Galaxy Nexus (cyanogenmod 10) using an otg cable
  • HTC Incredible (cyanogenmod 7.2) this returned both the internal and external. This device is kinda an oddball in that its internal largely goes unused as getExternalStorage() returns a path to the sdcard instead.

and some single storage devices that use an sdcard as their main storage

  • HTC G1 (cyanogenmod 6.1)
  • HTC G1 (stock)
  • HTC Vision/G2 (stock)

Excepting the Incredible all these devices only returned their removable storage. There are probably some extra checks I should be doing, but this is at least a bit better than any solution I've found thus far.

like image 141
Gnathonic Avatar answered Oct 06 '22 23:10

Gnathonic