Skip to content

Instantly share code, notes, and snippets.

@glnix
Forked from PauloLuan/GetExternalSdCardPath.java
Created January 26, 2016 00:15
Show Gist options
  • Save glnix/9073d3b2d896803c9317 to your computer and use it in GitHub Desktop.
Save glnix/9073d3b2d896803c9317 to your computer and use it in GitHub Desktop.
how to get the external sd card path on android.
public static String getExternalSdCardPath() {
String path = null;
File sdCardFile = null;
List<String> sdCardPossiblePath = Arrays.asList("external_sd", "ext_sd", "external", "extSdCard");
for (String sdPath : sdCardPossiblePath) {
File file = new File("/mnt/", sdPath);
if (file.isDirectory() && file.canWrite()) {
path = file.getAbsolutePath();
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
File testWritable = new File(path, "test_" + timeStamp);
if (testWritable.mkdirs()) {
testWritable.delete();
}
else {
path = null;
}
}
}
if (path != null) {
sdCardFile = new File(path);
}
else {
sdCardFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
}
return sdCardFile.getAbsolutePath();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment