I'm trying to query all the buckets in external uri with their count from contentResolver.
I tried two approaches and both have big drawbacks.
Approach 1:
Use
contentResolver's query API with bundles to useContentResolver.QUERY_ARG_SORT_COLUMNSand fetch the Bundles.Query each Bundle Id for the count of files in each bundle.
Drawback in this
- Which I feel is inefficient due to querying in loop.
- required BuildVersion.R :(
....
....
val queryBundle = Bundle().apply {
// SORTING
putStringArray(
ContentResolver.QUERY_ARG_SORT_COLUMNS,
...
// Group results by Bucket ID
putStringArray(
ContentResolver.QUERY_ARG_GROUP_COLUMNS,
arrayOf(MediaStore.Files.FileColumns.BUCKET_ID)
)
val cursor = context.contentResolver.query(externalUri, projection, queryBundle, null)
while (cursor.moveToNext()) {
val bucketId = cursor.getString(bucketIdIndex)
val bucketName = cursor.getString(bucketNameIndex)
// Query in looping :(
val countCursor = context.contentResolver.query(
externalUri,
projection,
"${MediaStore.Files.FileColumns.BUCKET_ID}=${bucketId}",
null,
null,
)
val bucketCount = countCursor?.count
....
....
}
...
Approach 2
- Query all the Medias of contentResolver and iterate every one
- segregate by BucketId and find the count.
Drawback
- Iterating all the files in contentResolver doesn't scaleable for large number of files
Is there an efficient way of achieving this with contentResolver?