Is there any method to get /storage/
directory. I tried with Environment.getExternalStorageDirectory()
but it returns /storage/emulated/0
i know i can use file.getParent()
but for some reason i cant use that. I just want a straight path to /storage/
directory using some function... Thanks in advance.
Asked
Active
Viewed 5,708 times
1

Sai
- 15,188
- 20
- 81
- 121
1 Answers
1
You can't and should not access that directory normally. But it seems you need more control on storage locations are available to your device.
For that you can use this (modified from https://stackoverflow.com/a/18871043/),
public List<String> getStorageDirectories() {
// Final set of paths
final ArrayList<String> finalPaths = new ArrayList<String>();
// Must add the ROOT directory
finalPaths.add("/");
// Primary physical SD-CARD (not emulated)
final String rawExternalStorage = System.getenv("EXTERNAL_STORAGE");
// All Secondary SD-CARDs (all exclude primary) separated by ":"
final String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
// Primary emulated SD-CARD
final String rawEmulatedStorageTarget = System.getenv("EMULATED_STORAGE_TARGET");
if (TextUtils.isEmpty(rawEmulatedStorageTarget)) {
// Device has physical external storage; use plain paths.
if (TextUtils.isEmpty(rawExternalStorage)) {
// EXTERNAL_STORAGE undefined; falling back to default.
finalPaths.add("/storage/sdcard0");
} else {
finalPaths.add(rawExternalStorage);
}
} else {
// Device has emulated storage; external storage paths should have
// userId burned into them.
final String rawUserId;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
rawUserId = "";
} else {
final String path = Environment.getExternalStorageDirectory().getAbsolutePath();
final String[] folders = DIR_SEPARATOR.split(path);
final String lastFolder = folders[folders.length - 1];
boolean isDigit = false;
try {
Integer.valueOf(lastFolder);
isDigit = true;
} catch (NumberFormatException ignored) {
}
rawUserId = isDigit ? lastFolder : "";
}
// /storage/emulated/0[1,2,...]
if (TextUtils.isEmpty(rawUserId)) {
finalPaths.add(rawEmulatedStorageTarget);
} else {
finalPaths.add(rawEmulatedStorageTarget + File.separator + rawUserId);
}
}
// Add all secondary storages
if (!TextUtils.isEmpty(rawSecondaryStoragesStr)) {
// All Secondary SD-CARDs splited into array
final String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
Collections.addAll(finalPaths, rawSecondaryStorages);
}
File usb = getUsbDrive();
if (usb != null && !finalPaths.contains(usb.getPath()))
finalPaths.add(usb.getPath());
return finalPaths;
}
And here is the method to get the USB drive attached to your device,
public File getUsbDrive() {
File parent;
parent = new File("/storage");
try {
for (File f : parent.listFiles()) {
if (f.exists() && f.getName().toLowerCase().contains("usb") && f.canExecute()) {
return f;
}
}
} catch (Exception e) {
}
parent = new File("/mnt/sdcard/usbStorage");
if (parent.exists() && parent.canExecute())
return (parent);
parent = new File("/mnt/sdcard/usb_storage");
if (parent.exists() && parent.canExecute())
return parent;
return null;
}
But remember that this is not an official way but a hack, so use it at your own risk.

General Grievance
- 4,555
- 31
- 31
- 45

Aritra Roy
- 15,355
- 10
- 73
- 107
-
'finalPaths.add("/storage/sdcard0");' Why would that be valid if all fails? 'all the USB drives'. That will only give one. And in some devices they are under /mnt. – greenapps Aug 18 '15 at 07:24
-
@greenapps As mentioned, this is a hack and is not guaranteed to work on all devices. We would have been lucky if there were some official ways. – Aritra Roy Aug 18 '15 at 07:48
-
You forgot to use getExternalFileDirs(). Dirs! And from the usb you do not deliver all. – greenapps Aug 18 '15 at 10:59