From b564da6b9111de89f8821f92a6f2a18f3a3a2c33 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 21 May 2017 09:17:52 +0200 Subject: [PATCH] catch exceptions thrown during media obtaining --- .../gallery/extensions/context.kt | 80 ++++++++++--------- 1 file changed, 44 insertions(+), 36 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 bc45c71ec..460c68aff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt @@ -139,53 +139,61 @@ 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)) - if (cur.moveToFirst()) { - var filename: String - var path: String - var dateTaken: Long - var dateModified: Long - var size: Long - var isImage: Boolean - var isVideo: Boolean + try { + if (cur.moveToFirst()) { + var filename: String + var path: String + var dateTaken: Long + var dateModified: Long + var size: Long + var isImage: Boolean + var isVideo: Boolean - do { - path = cur.getStringValue(MediaStore.Images.Media.DATA) - size = cur.getLongValue(MediaStore.Images.Media.SIZE) - if (size == 0L) { - size = File(path).length() - } - if (size <= 0L) { - continue - } + do { + try { + path = cur.getStringValue(MediaStore.Images.Media.DATA) + size = cur.getLongValue(MediaStore.Images.Media.SIZE) + if (size == 0L) { + size = File(path).length() + } - filename = cur.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: "" - if (filename.isEmpty()) - filename = path.getFilenameFromPath() + if (size <= 0L) { + continue + } - isImage = filename.isImageFast() || filename.isGif() - isVideo = if (isImage) false else filename.isVideoFast() + filename = cur.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: "" + if (filename.isEmpty()) + filename = path.getFilenameFromPath() - if (!isImage && !isVideo) - continue + isImage = filename.isImageFast() || filename.isGif() + isVideo = if (isImage) false else filename.isVideoFast() - if (isVideo && (isPickImage || showMedia == IMAGES)) - continue + if (!isImage && !isVideo) + continue - if (isImage && (isPickVideo || showMedia == VIDEOS)) - continue + if (isVideo && (isPickImage || showMedia == IMAGES)) + continue - if (!showHidden && filename.startsWith('.')) - continue + if (isImage && (isPickVideo || showMedia == VIDEOS)) + continue - dateTaken = cur.getLongValue(MediaStore.Images.Media.DATE_TAKEN) - dateModified = cur.getIntValue(MediaStore.Images.Media.DATE_MODIFIED) * 1000L + if (!showHidden && filename.startsWith('.')) + continue - val medium = Medium(filename, path, isVideo, dateModified, dateTaken, size) - curMedia.add(medium) - } while (cur.moveToNext()) + 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 + } + } while (cur.moveToNext()) + } + } finally { + cur.close() } - cur.close() Medium.sorting = config.getFileSorting(curPath) curMedia.sort()