class Gallery(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 findLocalImagesByAlbum(albumId: String, page: Int, pageSize: Int): Cursor? { val selection = "${MediaStore.Images.ImageColumns.BUCKET_ID} == ?" val selectionArgs = arrayOf( albumId ) // val selection = null // val selectionArgs = null 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 }}
❤️ Is this article helpful?
Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.
Do send some 💖 to @d_luaz or share this article.
✨ By Desmond Lua
A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz
👶 Apps I built
Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.