#167 improved volume fetch for Android 11+
This commit is contained in:
parent
9e1d141111
commit
cc1ca9f950
1 changed files with 20 additions and 3 deletions
|
@ -115,6 +115,15 @@ object StorageUtils {
|
|||
}
|
||||
|
||||
private fun findPrimaryVolumePath(context: Context): String? {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
val sm = context.getSystemService(Context.STORAGE_SERVICE) as? StorageManager
|
||||
val path = sm?.primaryStorageVolume?.directory?.path
|
||||
if (path != null) {
|
||||
return ensureTrailingSeparator(path)
|
||||
}
|
||||
}
|
||||
|
||||
// fallback
|
||||
try {
|
||||
// we want:
|
||||
// /storage/emulated/0/
|
||||
|
@ -130,9 +139,16 @@ object StorageUtils {
|
|||
}
|
||||
|
||||
private fun findVolumePaths(context: Context): Array<String> {
|
||||
// Final set of paths
|
||||
val paths = HashSet<String>()
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
val sm = context.getSystemService(Context.STORAGE_SERVICE) as? StorageManager
|
||||
val paths = sm?.storageVolumes?.mapNotNull { it.directory?.path }
|
||||
if (paths != null) {
|
||||
return paths.map(::ensureTrailingSeparator).toTypedArray()
|
||||
}
|
||||
}
|
||||
|
||||
// fallback
|
||||
val paths = HashSet<String>()
|
||||
try {
|
||||
// Primary emulated SD-CARD
|
||||
val rawEmulatedStorageTarget = System.getenv("EMULATED_STORAGE_TARGET") ?: ""
|
||||
|
@ -143,7 +159,8 @@ object StorageUtils {
|
|||
var validFiles: Boolean
|
||||
do {
|
||||
// `getExternalFilesDirs` sometimes include `null` when called right after getting read access
|
||||
// (e.g. on API 30 emulator) so we retry until the file system is ready
|
||||
// (e.g. on API 30 emulator) so we retry until the file system is ready.
|
||||
// TODO TLAD It can also include `null` when there is a faulty SD card.
|
||||
val externalFilesDirs = context.getExternalFilesDirs(null)
|
||||
validFiles = !externalFilesDirs.contains(null)
|
||||
if (validFiles) {
|
||||
|
|
Loading…
Reference in a new issue