You can use Environment.getExternalStorageDirectory()
this will give you the public external directory to read and write files.
sample code to create a text file in /storage/9016-4EF8/MyFolder/test.txt
File docsFolder = new File(Environment.getExternalStorageDirectory() + "/MyFolder");
if (!docsFolder.exists()) {
docsFolder.mkdir();
}
File file = new File(docsFolder.getAbsolutePath(),"test.txt");
Edit:
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();
}
Resources:
https://gist.github.com/PauloLuan/4bcecc086095bce28e22
https://www.codeproject.com/Questions/716256/find-path-of-external-SD-card