From faf1e19a87347d96345cdb2485a7520831a01dfe Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 24 May 2017 23:02:33 +0200 Subject: [PATCH] split the media getter into 2 functions --- .../gallery/extensions/context.kt | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt index b601ee56c..dd3039c87 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt @@ -47,9 +47,6 @@ fun Context.launchSettings() { val Context.config: Config get() = Config.newInstance(this) fun Context.getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean): ArrayList { - val curMedia = ArrayList() - val showMedia = config.showMedia - val showHidden = config.shouldShowHidden val projection = arrayOf(MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATE_TAKEN, @@ -61,7 +58,18 @@ fun Context.getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boo val selectionArgs = if (curPath.isEmpty()) null else arrayOf("$curPath/%", "$curPath/%/%") val cur = contentResolver.query(uri, projection, selection, selectionArgs, getSortingForFolder(curPath)) - try { + cur.use { cur -> + return parseCursor(this, cur, isPickImage, isPickVideo, curPath) + } +} + +private fun parseCursor(context: Context, cur: Cursor, isPickImage: Boolean, isPickVideo: Boolean, curPath: String): ArrayList { + val curMedia = ArrayList() + val config = context.config + val showMedia = config.showMedia + val showHidden = config.shouldShowHidden + + cur.use { cur -> if (cur.moveToFirst()) { var filename: String var path: String @@ -71,7 +79,7 @@ fun Context.getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boo var isImage: Boolean var isVideo: Boolean val excludedFolders = config.excludedFolders - val noMediaFolders = getNoMediaFolders() + val noMediaFolders = context.getNoMediaFolders() do { try { @@ -135,8 +143,6 @@ fun Context.getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boo } } while (cur.moveToNext()) } - } finally { - cur.close() } Medium.sorting = config.getFileSorting(curPath)