move cursor closing in the function using it

This commit is contained in:
tibbi 2017-05-28 23:17:06 +02:00
parent 18b714f9c3
commit a04d2d80b6

View file

@ -58,9 +58,7 @@ fun Context.getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boo
val selectionArgs = if (curPath.isEmpty()) null else arrayOf("$curPath/%", "$curPath/%/%") val selectionArgs = if (curPath.isEmpty()) null else arrayOf("$curPath/%", "$curPath/%/%")
val cur = contentResolver.query(uri, projection, selection, selectionArgs, getSortingForFolder(curPath)) val cur = contentResolver.query(uri, projection, selection, selectionArgs, getSortingForFolder(curPath))
cur.use { cur -> return parseCursor(this, cur, isPickImage, isPickVideo, curPath)
return parseCursor(this, cur, isPickImage, isPickVideo, curPath)
}
} }
private fun parseCursor(context: Context, cur: Cursor, isPickImage: Boolean, isPickVideo: Boolean, curPath: String): ArrayList<Medium> { private fun parseCursor(context: Context, cur: Cursor, isPickImage: Boolean, isPickVideo: Boolean, curPath: String): ArrayList<Medium> {
@ -69,78 +67,80 @@ private fun parseCursor(context: Context, cur: Cursor, isPickImage: Boolean, isP
val showMedia = config.showMedia val showMedia = config.showMedia
val showHidden = config.shouldShowHidden val showHidden = config.shouldShowHidden
if (cur.moveToFirst()) { cur.use { cur ->
var filename: String if (cur.moveToFirst()) {
var path: String var filename: String
var dateTaken: Long var path: String
var dateModified: Long var dateTaken: Long
var size: Long var dateModified: Long
var isImage: Boolean var size: Long
var isVideo: Boolean var isImage: Boolean
val excludedFolders = config.excludedFolders var isVideo: Boolean
val noMediaFolders = context.getNoMediaFolders() val excludedFolders = config.excludedFolders
val noMediaFolders = context.getNoMediaFolders()
do { do {
try { try {
path = cur.getStringValue(MediaStore.Images.Media.DATA) path = cur.getStringValue(MediaStore.Images.Media.DATA)
size = cur.getLongValue(MediaStore.Images.Media.SIZE) size = cur.getLongValue(MediaStore.Images.Media.SIZE)
if (size == 0L) { if (size == 0L) {
size = File(path).length() size = File(path).length()
}
if (size <= 0L) {
continue
}
filename = cur.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: ""
if (filename.isEmpty())
filename = path.getFilenameFromPath()
isImage = filename.isImageFast() || filename.isGif()
isVideo = if (isImage) false else filename.isVideoFast()
if (!isImage && !isVideo)
continue
if (isVideo && (isPickImage || showMedia == IMAGES))
continue
if (isImage && (isPickVideo || showMedia == VIDEOS))
continue
if (!showHidden && filename.startsWith('.'))
continue
var isExcluded = false
excludedFolders.forEach {
if (path.startsWith(it)) {
isExcluded = true
} }
}
if (!isExcluded && !showHidden) { if (size <= 0L) {
noMediaFolders.forEach { continue
}
filename = cur.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: ""
if (filename.isEmpty())
filename = path.getFilenameFromPath()
isImage = filename.isImageFast() || filename.isGif()
isVideo = if (isImage) false else filename.isVideoFast()
if (!isImage && !isVideo)
continue
if (isVideo && (isPickImage || showMedia == IMAGES))
continue
if (isImage && (isPickVideo || showMedia == VIDEOS))
continue
if (!showHidden && filename.startsWith('.'))
continue
var isExcluded = false
excludedFolders.forEach {
if (path.startsWith(it)) { if (path.startsWith(it)) {
isExcluded = true isExcluded = true
} }
} }
}
if (!isExcluded && !showHidden && path.contains("/.")) { if (!isExcluded && !showHidden) {
isExcluded = true noMediaFolders.forEach {
} if (path.startsWith(it)) {
isExcluded = true
}
}
}
if (!isExcluded) { if (!isExcluded && !showHidden && path.contains("/.")) {
dateTaken = cur.getLongValue(MediaStore.Images.Media.DATE_TAKEN) isExcluded = true
dateModified = cur.getIntValue(MediaStore.Images.Media.DATE_MODIFIED) * 1000L }
val medium = Medium(filename, path, isVideo, dateModified, dateTaken, size) if (!isExcluded) {
curMedia.add(medium) dateTaken = cur.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
dateModified = cur.getIntValue(MediaStore.Images.Media.DATE_MODIFIED) * 1000L
val medium = Medium(filename, path, isVideo, dateModified, dateTaken, size)
curMedia.add(medium)
}
} catch (e: Exception) {
continue
} }
} catch (e: Exception) { } while (cur.moveToNext())
continue }
}
} while (cur.moveToNext())
} }
Medium.sorting = config.getFileSorting(curPath) Medium.sorting = config.getFileSorting(curPath)