improve FixDateTaken, rescan paths missing from mediastore if necessary
This commit is contained in:
parent
255448fca2
commit
28981ac870
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue