Skip to content

Instantly share code, notes, and snippets.

@Priyansh-Kedia
Last active September 21, 2022 17:50
Show Gist options
  • Save Priyansh-Kedia/b09b87d587412ecc2a978c3fec00546f to your computer and use it in GitHub Desktop.
Save Priyansh-Kedia/b09b87d587412ecc2a978c3fec00546f to your computer and use it in GitHub Desktop.
Document Repo for Pagination in MediaStore API
suspend fun getImages(count: Int, start: Int): Three<MutableList<Three<Uri?, String?, Date>>, Boolean, Int> {
val imagesList = mutableListOf<Three<Uri?, String?, Date>>()
var index = start
return withContext(Dispatchers.IO) {
while (imageCursor?.moveToPosition(i) == true) {
val id = imageIdColumn?.let { imageCursor.getLong(it) }
val dateModified = Date(TimeUnit.SECONDS.toMillis(imageCursor.getLong(imageDateModifiedColumn ?: 0)))
val displayName = imageDisplayNameColumn?.let { imageCursor.getString(it) }
val contentUri = id?.let {
ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
it
)
}
index++
contentUri?.let { imagesList.add(Three(contentUri, displayName, dateModified)) }
if (index == count)
break
}
val areAllLoaded = index == imagesToLoad
return@withContext Three(imagesList, areAllLoaded, index)
}
}
@jeyk-digikraft
Copy link

imageCursor, are missed ?

@Priyansh-Kedia
Copy link
Author

Hello @jeyk-digikraft
ImageCursor is created using the default method. Adding the code here too.

private val imageCursor = context.contentResolver.query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, ImagePicker.imageProjection, null, null, ImagePicker.imageSortOrder )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment