change the way we obtain images

This commit is contained in:
tibbi 2017-02-23 23:57:49 +01:00
parent a8c5cc1fdd
commit 30c7b9b055
3 changed files with 15 additions and 40 deletions

View file

@ -26,9 +26,9 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
val media = ArrayList<Medium>() val media = ArrayList<Medium>()
val showMedia = config.showMedia val showMedia = config.showMedia
val fileSorting = config.fileSorting val fileSorting = config.fileSorting
val parents = context.getParents(isPickImage, isPickVideo) val parents = context.getParents()
parents.map { File(it).listFiles() } parents.mapNotNull { File(it).listFiles() }
.forEach { .forEach {
for (file in it) { for (file in it) {
val isImage = file.isImageFast() || file.isGif() val isImage = file.isImageFast() || file.isGif()

View file

@ -30,7 +30,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
val media = ArrayList<Medium>() val media = ArrayList<Medium>()
if (showAll) { if (showAll) {
val parents = context.getParents(isPickImage, isPickVideo) val parents = context.getParents()
for (parent in parents) { for (parent in parents) {
media.addAll(getFilesFrom(parent)) media.addAll(getFilesFrom(parent))
} }

View file

@ -11,9 +11,7 @@ import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SettingsActivity import com.simplemobiletools.gallery.activities.SettingsActivity
import com.simplemobiletools.gallery.helpers.Config import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.helpers.IMAGES
import com.simplemobiletools.gallery.helpers.NOMEDIA import com.simplemobiletools.gallery.helpers.NOMEDIA
import com.simplemobiletools.gallery.helpers.VIDEOS
import java.io.File import java.io.File
import java.util.* import java.util.*
@ -51,55 +49,34 @@ fun Context.launchSettings() {
startActivity(Intent(this, SettingsActivity::class.java)) startActivity(Intent(this, SettingsActivity::class.java))
} }
fun Context.getParents(isPickImage: Boolean, isPickVideo: Boolean): ArrayList<String> { fun Context.getParents(): ArrayList<String> {
val uri = MediaStore.Files.getContentUri("external") val uri = MediaStore.Files.getContentUri("external")
val where = "${getWhereCondition(isPickImage, isPickVideo)} GROUP BY ( ${MediaStore.Files.FileColumns.PARENT} " val columns = arrayOf(MediaStore.Images.Media.DATA)
val args = getArgs(isPickImage, isPickVideo)
val columns = arrayOf(MediaStore.Files.FileColumns.PARENT, MediaStore.Images.Media.DATA)
var cursor: Cursor? = null var cursor: Cursor? = null
val parents = ArrayList<String>() val parents = ArrayList<String>()
val parentsSet = HashSet<String>()
try { try {
cursor = contentResolver.query(uri, columns, where, args, null) cursor = contentResolver.query(uri, columns, null, null, null)
if (cursor?.moveToFirst() == true) { if (cursor?.moveToFirst() == true) {
do { do {
val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: "" val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: continue
parents.add(File(curPath).parent) val parent = File(curPath).parent ?: continue
parentsSet.add(parent)
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
} finally { } finally {
cursor?.close() cursor?.close()
} }
val filtered = ArrayList<String>() parentsSet.filterTo(parents, { hasImageVideoGif(File(it)) })
parents.mapNotNullTo(filtered, { it })
if (config.showHiddenFolders) { if (config.showHiddenFolders) {
filtered.addAll(getNoMediaFolders()) parents.addAll(getNoMediaFolders())
} else { } else {
removeNoMediaFolders(filtered) removeNoMediaFolders(parents)
}
return filtered
}
fun Context.getWhereCondition(isPickImage: Boolean, isPickVideo: Boolean): String {
val showMedia = config.showMedia
return if ((isPickImage || showMedia == IMAGES) || (isPickVideo || showMedia == VIDEOS)) {
"${MediaStore.Files.FileColumns.MEDIA_TYPE} = ?)"
} else {
"${MediaStore.Files.FileColumns.MEDIA_TYPE} = ? OR ${MediaStore.Files.FileColumns.MEDIA_TYPE} = ?)"
}
}
fun Context.getArgs(isPickImage: Boolean, isPickVideo: Boolean): Array<String> {
val showMedia = config.showMedia
return if (isPickImage || showMedia == IMAGES) {
arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString())
} else if (isPickVideo || showMedia == VIDEOS) {
arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO.toString())
} else {
arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString(), MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO.toString())
} }
return parents
} }
private fun removeNoMediaFolders(paths: MutableList<String>) { private fun removeNoMediaFolders(paths: MutableList<String>) {
@ -146,9 +123,7 @@ fun Context.getNoMediaFolders(): ArrayList<String> {
fun hasImageVideoGif(dir: File): Boolean { fun hasImageVideoGif(dir: File): Boolean {
if (dir.isDirectory) { if (dir.isDirectory) {
dir.listFiles() dir.listFiles()?.filter(File::isImageVideoGif)?.any { return true }
.filter(File::isImageVideoGif)
.forEach { return true }
} }
return false return false
} }