From 8476369066e7ef0f557bb9504e94f0db9df0f48b Mon Sep 17 00:00:00 2001 From: Anil Kumar Beesetti <66936649+anilbeesetti@users.noreply.github.com> Date: Sun, 30 Jul 2023 12:12:48 +0530 Subject: [PATCH] Fix: Force scan media files not working on api levels below Q (#474) --- .../core/common/extensions/Context.kt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/core/common/src/main/java/dev/anilbeesetti/nextplayer/core/common/extensions/Context.kt b/core/common/src/main/java/dev/anilbeesetti/nextplayer/core/common/extensions/Context.kt index e96269ac8..db5ceff13 100644 --- a/core/common/src/main/java/dev/anilbeesetti/nextplayer/core/common/extensions/Context.kt +++ b/core/common/src/main/java/dev/anilbeesetti/nextplayer/core/common/extensions/Context.kt @@ -197,18 +197,25 @@ fun Context.showToast(string: String, duration: Int = Toast.LENGTH_SHORT) { } fun Context.scanPaths(paths: List, callback: ((String?, Uri?) -> Unit)? = null) { - MediaScannerConnection.scanFile( - this, - paths.toTypedArray(), - arrayOf("video/*"), - callback - ) + MediaScannerConnection.scanFile(this, paths.toTypedArray(), arrayOf("video/*"), callback) +} + +fun Context.scanPath(file: File) { + if (file.isDirectory) { + file.listFiles()?.forEach { scanPath(it) } + } else { + scanPaths(listOf(file.path)) + } } fun Context.scanStorage(callback: ((String?, Uri?) -> Unit)? = null) { val storagePath = Environment.getExternalStorageDirectory()?.path if (storagePath != null) { - scanPaths(listOf(storagePath), callback) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + scanPaths(listOf(storagePath), callback) + } else { + scanPath(File(storagePath)) + } } else { callback?.invoke(null, null) }