Skip to content

Commit

Permalink
Fix: Force scan media files not working on api levels below Q (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
anilbeesetti authored Jul 30, 2023
1 parent 1db0a06 commit 8476369
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,25 @@ fun Context.showToast(string: String, duration: Int = Toast.LENGTH_SHORT) {
}

fun Context.scanPaths(paths: List<String>, 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)
}
Expand Down

0 comments on commit 8476369

Please sign in to comment.