Creating a Utility to get the path to External Removable Storage every time

Recently, I needed to write some data strictly on an external, removable media (aka SDCard). For lower devices without an β€œexternal” internal storage (you know what I mean) this is not a problem since

Environment.getExternalStorageDirectory().getAbsolutePath() ,

which returns

/mnt/sdcard

- also the path to sdcard. However, on more expensive phones with built-in flash memory (e.g. sgs 2) /mnt/sdcard , which is returned from Environment.getExternalStorageDirectory().getAbsolutePath() is just an internal storage method. Real way to sdcard: `

/ highway / sdcard / external_sd`.

This does not stop here, however, as when I tried with sgs2, which was updated to ICS, the path to the SD card is now

/mnt/emmc .

Now I'm trying to create a general Utility class called ExternalStorage, which has a static method that returns the path to the sdcard root (as well as some other methods to get the total space, free space, space consumed by a specific dir, etc.) so far I have it:

Updated Version:

 public static String getRemovableStoragePath() { File f = null; // Log.d(TAG, "Build.DEVICE: " + Build.DEVICE); // Log.d(TAG, "Build.MANUFACTURER: " + Build.MANUFACTURER); // Log.d(TAG, "Build.MODEL: " + Build.MODEL); if (Build.VERSION.RELEASE.startsWith("4")) { f = new File("/mnt/emmc"); } if (Build.MODEL.toLowerCase().startsWith("mele")) { return "/mnt/extern_sd0"; } if (Build.DEVICE.equals("nuclear-zoop") || Build.DEVICE.equals("nuclear-f900")) { f = new File("/mnt/extsd"); } String path = Environment.getExternalStorageDirectory().getAbsolutePath(); String extendedPath = ""; if (Build.DEVICE.toLowerCase().contains("samsung") || Build.MANUFACTURER.toLowerCase().contains("samsung")) { extendedPath = "/external_sd/"; try { f = new File(path + extendedPath); if (f.exists() && f.isDirectory()) { return f.getAbsolutePath(); } else if (Build.MODEL.toLowerCase().contains("gt-i9300")) { extendedPath = "/mnt/extSdCard/"; try { f = new File(extendedPath); if (f.exists() && f.isDirectory()) { return f.getAbsolutePath(); } } catch (Exception e) { // continue execution } } else { extendedPath = "/sd"; } } catch (Exception e) { // contine execution } } else if (Build.DEVICE.toLowerCase().contains("e0") || Build.MANUFACTURER.toLowerCase().contains("LGE")) { extendedPath = "/_ExternalSD/"; } else if (Build.MANUFACTURER.toLowerCase().contains("motorola") || Build.DEVICE.toLowerCase().contains("olympus")) { f = new File("/mnt/sdcard-ext"); } try { if (!extendedPath.equals("")) { f = new File(path + extendedPath); } if (f.exists() && f.isDirectory()) { // Log.d(TAG, "path: " + f.getAbsolutePath()); return f.getAbsolutePath(); } else { } } catch (Exception e) { // e.printStackTrace(); // f is probably null. no need to print stacktrace. return path; } return path; // return Environment.getExternalStorageDirectory().getAbsolutePath(); } 

Has anyone done this already? I know that I am missing many mfg-specific paths, so I need help completing this utility to support as many Android phones as possible.

+4
source share
1 answer

I'm not sure how reliable it would be, but since the removable SD card contains the LOST.DIR folder at its root and is recreated at boot (in cases where the user can delete it) ...

  File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath()); for (File sub : root.listFiles()) { if (new File(sub + "/lost.dir").exists()) { // 'sub' is probably the removable SD break; } } 

If "sub" is in the LOST.DIR folder, it might want to write its value to sharedPrefs so that this search is performed only once (if there is no value stored), and then it is simply extracted from prefs, OR that the application knows this path, even if the user has deleted LOST.DIR and it does not currently exist.

+2
source

All Articles