I am not able to find a way which runs all device and return base path for sdcard. I found the following way to fetch all files with filter "storage" and then check if it is external file or not. It is working fine on few devices.
File root = new File("/storage/");
File dirs[] = root.listFiles();
for (File dir : dirs) {
if (Environment.isExternalStorageRemovable(dir.getAbsoluteFile())) {
return dir.getPath();
}
}
Now i got a device in my hand whose sdcard path is "roor/Removable/MicroSd , through above code i am not able read this card. I have gone through many links but not able to find standard way. I also have tried following but this does not return actual address.
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
I suppose you wish access removable SDCard . The standard way to get the path of that SDCard is: Context.getExternalFilesDirs() . Normally SDCard path is the last one and you have to strip out the private subpath (Android/...) . Better check if SDCard is mounted too.
String path = null;
File[] dirs = getContext().getExternalFilesDirs(null);
if (dirs!=null && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
path = dirs[dirs.length-1].getAbsolutePath();
path = path.substring(0, path.indexOf("Android"));
}
return path;