adding a couple improvements to fetching SD card path

This commit is contained in:
tibbi 2019-12-25 19:28:35 +01:00
parent 2718207ed1
commit 46a4fbd6e6
2 changed files with 6 additions and 13 deletions

View file

@ -7,7 +7,7 @@ buildscript {
propMinSdkVersion = 21
propTargetSdkVersion = propCompileSdkVersion
propVersionCode = 1
propVersionName = '5.21.1'
propVersionName = '5.21.2'
kotlin_version = '1.3.61'
}

View file

@ -24,29 +24,22 @@ import java.util.regex.Pattern
// http://stackoverflow.com/a/40582634/1967672
fun Context.getSDCardPath(): String {
val directories = getStorageDirectories().filter {
it != getInternalStoragePath() && !it.equals("/storage/emulated/0", true) && (baseConfig.OTGPartition.isEmpty() || !it.endsWith(baseConfig.OTGPartition))
!it.equals(getInternalStoragePath()) && !it.equals("/storage/emulated/0", true) && (baseConfig.OTGPartition.isEmpty() || !it.endsWith(baseConfig.OTGPartition))
}
val fullSDpattern = Pattern.compile(SD_OTG_PATTERN)
var sdCardPath = directories.firstOrNull { fullSDpattern.matcher(it).matches() }
?: directories.firstOrNull { !physicalPaths.contains(it.toLowerCase()) } ?: ""
// on some devices no method retrieved any SD card path, so test if its not sdcard1 by any chance. It happened on an Android 5.1
if (sdCardPath.trimEnd('/').isEmpty()) {
val file = File("/storage/sdcard1")
if (file.exists()) {
return file.absolutePath
}
sdCardPath = directories.firstOrNull() ?: ""
}
if (sdCardPath.isEmpty()) {
val SDpattern = Pattern.compile(SD_OTG_SHORT)
try {
File("/storage").listFiles()?.forEach {
if (SDpattern.matcher(it.name).matches()) {
sdCardPath = "/storage/${it.name}"
val potentialPath = "/storage/${it.name}"
if (!potentialPath.equals(Environment.getExternalStorageDirectory().absolutePath, true)) {
sdCardPath = potentialPath
}
}
}
} catch (e: Exception) {