improve FixDateTaken, rescan paths missing from mediastore if necessary

This commit is contained in:
tibbi 2019-09-07 17:11:20 +02:00
parent 255448fca2
commit 28981ac870
2 changed files with 41 additions and 6 deletions

View File

@ -348,12 +348,13 @@ fun Activity.hasNavBar(): Boolean {
return (realDisplayMetrics.widthPixels - displayMetrics.widthPixels > 0) || (realDisplayMetrics.heightPixels - displayMetrics.heightPixels > 0)
}
fun Activity.fixDateTaken(paths: ArrayList<String>, showToasts: Boolean, callback: (() -> Unit)? = null) {
fun Activity.fixDateTaken(paths: ArrayList<String>, showToasts: Boolean, hasRescanned: Boolean = false, callback: (() -> Unit)? = null) {
val BATCH_SIZE = 50
if (showToasts) {
toast(R.string.fixing)
}
val pathsToRescan = ArrayList<String>()
try {
var didUpdateFile = false
val operations = ArrayList<ContentProviderOperation>()
@ -388,6 +389,10 @@ fun Activity.fixDateTaken(paths: ArrayList<String>, showToasts: Boolean, callbac
mediumDao.updateFavoriteDateTaken(path, timestamp)
didUpdateFile = true
if (!hasRescanned && getFileDateTaken(path) == 0L) {
pathsToRescan.add(path)
}
}
val resultSize = contentResolver.applyBatch(MediaStore.AUTHORITY, operations).size
@ -395,6 +400,7 @@ fun Activity.fixDateTaken(paths: ArrayList<String>, showToasts: Boolean, callbac
didUpdateFile = false
}
if (hasRescanned || pathsToRescan.isEmpty()) {
runOnUiThread {
if (showToasts) {
toast(if (didUpdateFile) R.string.dates_fixed_successfully else R.string.unknown_error_occurred)
@ -402,6 +408,11 @@ fun Activity.fixDateTaken(paths: ArrayList<String>, showToasts: Boolean, callbac
callback?.invoke()
}
} else {
rescanPaths(pathsToRescan) {
fixDateTaken(paths, showToasts, true)
}
}
}
} catch (e: Exception) {
if (showToasts) {

View File

@ -852,3 +852,27 @@ fun Context.updateDirectoryPath(path: String) {
val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, isSortingAscending, getProperFileSize)
updateDBDirectory(directory, galleryDB.DirectoryDao())
}
fun Context.getFileDateTaken(path: String): Long {
val projection = arrayOf(
MediaStore.Images.Media.DATE_TAKEN
)
val uri = MediaStore.Files.getContentUri("external")
val selection = "${MediaStore.Images.Media.DATA} = ?"
val selectionArgs = arrayOf(path)
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
do {
try {
return cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
} catch (e: Exception) {
}
} while (cursor.moveToNext())
}
}
return 0L
}