Android: How to query a list of bucket name

Viewed 15568

I want to retrieve only the name of the bucket (Albums). E.g. Camera, Download etc but not a list of Camera, Download etc from all the the photos so how do I retrieve one row each for each bucket name?

What I mean like in Gallery Application, you have albums first e.g. Camera. When you clicked on it, it show all the photos of the camera.

I can query the photos in a Camera Roll with the Where clause of the query. But what if I wanted only the name of each of the albums' name and not the photos, is it possible to query that? If I query all the photos and take only one row per set of photos, then it will be time consuming.

Please Help

5 Answers

Use following function to get albums of Video or Images :

   /*
    *
    *   Author : @nieldeokar
    *   mediaType could be one of
    *
    *   public static final int MEDIA_TYPE_IMAGE = 1;
    *
    *   public static final int MEDIA_TYPE_VIDEO = 3;
    *
    *   from android.provider.MediaStore class
    *
    */
fun getAlbumList(mediaType: Int, contentResolver: ContentResolver) {
    val countColumnName = "count"
    var contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    if (mediaType == MEDIA_TYPE_VIDEO) {
        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
    }

    val projection = arrayOf(ImageColumns.BUCKET_ID, ImageColumns.BUCKET_DISPLAY_NAME, ImageColumns.DATE_TAKEN, ImageColumns.DATA)
    val bucketGroupBy = "1) GROUP BY ${ImageColumns.BUCKET_ID}, (${ImageColumns.BUCKET_DISPLAY_NAME}"
    val bucketOrderBy = MediaStore.Images.Media.DATE_MODIFIED + " DESC"

    val cursor = contentResolver.query(contentUri, projection, bucketGroupBy, null, bucketOrderBy)


    if (cursor != null) {
        while (cursor.moveToNext()) {
            val bucketId = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.BUCKET_ID))
            val name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME))
            val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)) // Thumb image path

            val selection = MediaStore.Images.Media.BUCKET_ID + "='" + bucketId + "'"

            val countCursor = contentResolver.query(contentUri, arrayOf( "count(" + MediaStore.Images.ImageColumns._ID + ")"), selection, null, null)

            var count = 0
            if (countCursor != null) {
                countCursor.moveToFirst()
                count = countCursor.getInt(0)
                countCursor.close()
            }

            Log.d("AlbumScanner", "bucketId : $bucketId | name : $name | count : $count | path : $path")
        }
        cursor.close()

    }

}

This forms a SQL Statement as :

SELECT bucket_id, bucket_display_name, datetaken, _data FROM images WHERE (1) GROUP BY bucket_id,(bucket_display_name) ORDER BY date_modified DESC

UPDATE : As @PerracoLabs pointed out, This code needs to be improved for targeting Android Q.

Related