Ok, I have searched the forums high and low, and am unable to find an acceptable answer to test if the SD card is properly mounted and is writable on Android 7 devices. (There are many answers, but none that actually work).
I understand that one must use Environment.getExternalStorageState(), as per my code below:
pathName=context.getFilesDir().getAbsolutePath()+"/"+SavePath;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
pathName= Objects.requireNonNull(context.getExternalFilesDir(SavePath)).getAbsolutePath();
BUT...
Regardless whether the SD card is mounted or not, this always return TRUE, simply because the SD card is mounted as an emulated resource, NOT a removable resource.
I can use Environment.isExternalStorageEmulated() to check this state, BUT it still doesn't tell me whether the SD card is available or not. (i.e.: isExternalStorageEmulated() will always return true, whether there exists an SD card or not, because it is set by the file system on startup and doesn't seem to care whether there is a physical SD card in the slot or not).
Other than trying to write to the SD card, and capturing the exception error if it doesn't exist, is there any way to tell if an SD card is present or not BEFORE I attempt to write to it?
EDIT:
After reviewing the proposed solutions, this is the best I can come up with. This seems very clunky. Any ideas on how to improve this?
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
if (Environment.isExternalStorageEmulated()){
File[] storage=getExternalFilesDirs(null);
// Find the first non-emulated storage space
for (File file: storage){
if (!file.toString().contains("emulated/"))
System.out.println(file.toString());
}
}
}