Android ContentResolver.query From Date to Date

December 17, 2021

MediaStore.Images.ImageColumns.DATE_TAKEN is stored as Cursor#FIELD_TYPE_INTEGER (number of milliseconds since 1970-01-01T00:00:00Z)

class Gallert(context: Context) {

    private val contentResolver by lazy {
        context.contentResolver
    }


    private val imageCollection: Uri by lazy {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            MediaStore.Images.Media.getContentUri(
                MediaStore.VOLUME_EXTERNAL
            )
        } else {
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        }
    }

    private val imageProjections by lazy {
       arrayOf(
            MediaStore.Images.ImageColumns._ID,
            // MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
            MediaStore.Images.ImageColumns.DATE_TAKEN,
            MediaStore.Images.ImageColumns.DISPLAY_NAME,
            MediaStore.Images.ImageColumns.ORIENTATION,
            MediaStore.Images.ImageColumns.WIDTH,
            MediaStore.Images.ImageColumns.HEIGHT,
            MediaStore.Images.ImageColumns.SIZE,
            @Suppress("DEPRECATION")
            MediaStore.Images.ImageColumns.LATITUDE,
            @Suppress("DEPRECATION")
            MediaStore.Images.ImageColumns.LONGITUDE
        )
    }

    fun findLocalImagesByDate(date: LocalDate, page: Int, pageSize: Int): Cursor? {
        val selection = "${MediaStore.Images.ImageColumns.DATE_TAKEN} >= ? AND ${MediaStore.Images.ImageColumns.DATE_TAKEN} <= ?"
        val selectionArgs = arrayOf<String>(
            date.atStartOfDay().toMillis().toString(),
            date.atEndOfDay().toMillis().toString()
        )

        val limit = pageSize
        val offset = (page - 1) * pageSize
        val sort = MediaStore.Images.ImageColumns.DATE_TAKEN

        val query = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
            val bundle = Bundle().apply {
                // selection
                putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection)
                putStringArray(
                    ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS,
                    selectionArgs
                )
                // sort
                putString(
                    ContentResolver.QUERY_ARG_SORT_COLUMNS,
                    sort
                )
                putInt(
                    ContentResolver.QUERY_ARG_SORT_DIRECTION,
                    ContentResolver.QUERY_SORT_DIRECTION_DESCENDING
                )
                // limit, offset
                putInt(ContentResolver.QUERY_ARG_LIMIT, limit)
                putInt(ContentResolver.QUERY_ARG_OFFSET, offset)
            }
            contentResolver.query(imageCollection, imageProjections, bundle, null)
        }
        else {
            contentResolver.query(imageCollection, imageProjections, selection, selectionArgs, "$sort ASC LIMIT $limit OFFSET $offset")
        }

        return query
    }
}

NOTE: Refer LocalDateTime.toMillis

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.