0

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 use ContentResolver.QUERY_ARG_SORT_COLUMNS and 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?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.