add a helper function for fast getting folders with media
This commit is contained in:
parent
30c7b9b055
commit
513694c117
1 changed files with 30 additions and 4 deletions
|
@ -49,13 +49,36 @@ fun Context.launchSettings() {
|
|||
startActivity(Intent(this, SettingsActivity::class.java))
|
||||
}
|
||||
|
||||
fun Context.getMediaFolders(): Set<String> {
|
||||
val uri = MediaStore.Files.getContentUri("external")
|
||||
val where = "${MediaStore.Files.FileColumns.MEDIA_TYPE} = ? OR ${MediaStore.Files.FileColumns.MEDIA_TYPE} = ?"
|
||||
val args = arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString(), MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO.toString())
|
||||
val columns = arrayOf(MediaStore.Files.FileColumns.PARENT, MediaStore.Images.Media.DATA)
|
||||
val parentsSet = HashSet<String>()
|
||||
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = contentResolver.query(uri, columns, where, args, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
do {
|
||||
val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: ""
|
||||
parentsSet.add(File(curPath).parent)
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
|
||||
return parentsSet
|
||||
}
|
||||
|
||||
fun Context.getParents(): ArrayList<String> {
|
||||
val uri = MediaStore.Files.getContentUri("external")
|
||||
val columns = arrayOf(MediaStore.Images.Media.DATA)
|
||||
var cursor: Cursor? = null
|
||||
val parents = ArrayList<String>()
|
||||
|
||||
val parentsSet = HashSet<String>()
|
||||
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = contentResolver.query(uri, columns, null, null, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
|
@ -69,13 +92,15 @@ fun Context.getParents(): ArrayList<String> {
|
|||
cursor?.close()
|
||||
}
|
||||
|
||||
parentsSet.filterTo(parents, { hasImageVideoGif(File(it)) })
|
||||
val mediaFolders = getMediaFolders()
|
||||
parentsSet.filterTo(parents, { mediaFolders.contains(it) || hasImageVideoGif(File(it)) })
|
||||
|
||||
if (config.showHiddenFolders) {
|
||||
parents.addAll(getNoMediaFolders())
|
||||
} else {
|
||||
removeNoMediaFolders(parents)
|
||||
}
|
||||
|
||||
return parents
|
||||
}
|
||||
|
||||
|
@ -123,7 +148,8 @@ fun Context.getNoMediaFolders(): ArrayList<String> {
|
|||
|
||||
fun hasImageVideoGif(dir: File): Boolean {
|
||||
if (dir.isDirectory) {
|
||||
dir.listFiles()?.filter(File::isImageVideoGif)?.any { return true }
|
||||
val files = dir.listFiles() ?: return false
|
||||
files.filter(File::isImageVideoGif).any { return true }
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue