diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index 955ce9eed..8f060f922 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -226,7 +226,7 @@ class MediaFetcher(val context: Context) { val checkFileExistence = config.fileLoadingPriority == PRIORITY_VALIDITY val showHidden = config.shouldShowHidden val showPortraits = filterMedia and TYPE_PORTRAITS != 0 - val dateTakens = if (getProperDateTaken && folder != FAVORITES && !isRecycleBin) getFolderDateTakens(folder) else HashMap() + val dateTakens = if (getProperDateTaken && !isRecycleBin) getFolderDateTakens(folder) else HashMap() val files = when (folder) { FAVORITES -> favoritePaths.filter { showHidden || !it.contains("/.") }.map { File(it) }.toMutableList() as ArrayList @@ -302,7 +302,7 @@ class MediaFetcher(val context: Context) { val videoDuration = if (getVideoDurations && isVideo) path.getVideoDuration() else 0 if (getProperDateTaken) { - dateTaken = dateTakens.remove(filename) ?: lastModified + dateTaken = dateTakens.remove(path) ?: lastModified } val type = when { @@ -390,34 +390,42 @@ class MediaFetcher(val context: Context) { } private fun getFolderDateTakens(folder: String): HashMap { - val projection = arrayOf( - MediaStore.Images.Media.DISPLAY_NAME, - MediaStore.Images.Media.DATE_TAKEN - ) - - val uri = MediaStore.Files.getContentUri("external") - val selection = "${MediaStore.Images.Media.DATA} LIKE ? AND ${MediaStore.Images.Media.DATA} NOT LIKE ?" - val selectionArgs = arrayOf("$folder/%", "$folder/%/%") - val dateTakens = HashMap() - val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) - cursor?.use { - if (cursor.moveToFirst()) { - do { - try { - val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN) - if (dateTaken != 0L) { - val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) - dateTakens[name] = dateTaken + if (folder != FAVORITES) { + val projection = arrayOf( + MediaStore.Images.Media.DISPLAY_NAME, + MediaStore.Images.Media.DATE_TAKEN + ) + + val uri = MediaStore.Files.getContentUri("external") + val selection = "${MediaStore.Images.Media.DATA} LIKE ? AND ${MediaStore.Images.Media.DATA} NOT LIKE ?" + val selectionArgs = arrayOf("$folder/%", "$folder/%/%") + + val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) + cursor?.use { + if (cursor.moveToFirst()) { + do { + try { + val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN) + if (dateTaken != 0L) { + val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) + dateTakens["$folder/$name"] = dateTaken + } + } catch (e: Exception) { } - } catch (e: Exception) { - } - } while (cursor.moveToNext()) + } while (cursor.moveToNext()) + } } } - context.dateTakensDB.getDateTakensFromPath(folder).forEach { - dateTakens[it.filename] = it.taken + val dateTakenValues = if (folder == FAVORITES) { + context.dateTakensDB.getAllDateTakens() + } else { + context.dateTakensDB.getDateTakensFromPath(folder) + } + + dateTakenValues.forEach { + dateTakens[it.fullPath] = it.taken } return dateTakens @@ -460,7 +468,6 @@ class MediaFetcher(val context: Context) { } fun groupMedia(media: ArrayList, path: String): ArrayList { - val mediumGroups = LinkedHashMap>() val pathToCheck = if (path.isEmpty()) SHOW_ALL else path val currentGrouping = context.config.getFolderGrouping(pathToCheck) if (currentGrouping and GROUP_BY_NONE != 0) { @@ -473,6 +480,7 @@ class MediaFetcher(val context: Context) { return thumbnailItems } + val mediumGroups = LinkedHashMap>() media.forEach { val key = it.getGroupingKey(currentGrouping) if (!mediumGroups.containsKey(key)) {