adding a helper function for fetching media store IDs from file paths

This commit is contained in:
tibbi 2021-10-08 21:48:34 +02:00
parent e268927756
commit 3781ee1f04
2 changed files with 41 additions and 0 deletions

View file

@ -1045,3 +1045,18 @@ fun Context.getFileDateTaken(path: String): Long {
return 0L return 0L
} }
fun Context.getFileUrisFromFileDirItems(fileDirItems: ArrayList<FileDirItem>): ArrayList<Uri> {
val fileUris = ArrayList<Uri>()
val allIds = MediaFetcher(this).getMediaStoreIds()
val filePaths = fileDirItems.map { it.path.lowercase(Locale.getDefault()) }
for ((filePath, mediaStoreId) in allIds) {
if (filePaths.contains(filePath.lowercase(Locale.getDefault()))) {
val baseUri = getFileUri(filePath)
val uri = ContentUris.withAppendedId(baseUri, mediaStoreId)
fileUris.add(uri)
}
}
return fileUris
}

View file

@ -606,6 +606,32 @@ class MediaFetcher(val context: Context) {
return sizes return sizes
} }
fun getMediaStoreIds(): HashMap<String, Long> {
val ids = HashMap<String, Long>()
val projection = arrayOf(
Images.Media.DATA,
Images.Media._ID
)
val uri = Files.getContentUri("external")
try {
context.queryCursor(uri, projection) { cursor ->
try {
val id = cursor.getLongValue(Images.Media._ID)
if (id != 0L) {
val path = cursor.getStringValue(Images.Media.DATA)
ids[path] = id
}
} catch (e: Exception) {
}
}
} catch (e: Exception) {
}
return ids
}
fun sortMedia(media: ArrayList<Medium>, sorting: Int) { fun sortMedia(media: ArrayList<Medium>, sorting: Int) {
if (sorting and SORT_BY_RANDOM != 0) { if (sorting and SORT_BY_RANDOM != 0) {
media.shuffle() media.shuffle()