limiting number of rows in a ContentResolver.query() function

后端 未结 3 675
借酒劲吻你
借酒劲吻你 2020-12-30 19:22

Is there a way to limit the number of returned rows to a cursor? I have a phone with about 4000 contacts, I just need some of them.

this is the code i\'m using

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 19:54

    From Android 11, that above solution will not work, you can try this one to fetch the data.

        /**
     * Call to fetch all media on device, it but be called synchronously since function is called on a background thread
     */
    private fun fetchGalleryImages(
        context: Context,
        offset: Int,
        limit: Int
    ): List {
        val galleryImageUrls = mutableListOf()
        try {
            if (EasyPermissions.hasPermissions(
                    context,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
            ) {
                // Define the columns that will be fetched
                val projection = arrayOf(
                    MediaStore.Files.FileColumns._ID,
                    MediaStore.Files.FileColumns.DATA,
                    MediaStore.Files.FileColumns.DATE_ADDED,
                    MediaStore.Files.FileColumns.MEDIA_TYPE,
                    MediaStore.Files.FileColumns.MIME_TYPE,
                    MediaStore.Files.FileColumns.TITLE,
                    MediaStore.Video.Media.DURATION
                )
                val selection =
                    "${MediaStore.Files.FileColumns.MEDIA_TYPE} = ? OR ${MediaStore.Files.FileColumns.MEDIA_TYPE} = ?"
                val selectionArgs = arrayOf(
                    MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString(),
                    MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO.toString()
                )
                /**
                 * Change the way to fetch Media Store
                 */
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    // Get All data in Cursor by sorting in DESC order
                    context.contentResolver.query(
                        contentUri(),
                        projection,
                        Bundle().apply {
                            // Limit & Offset
                            putInt(ContentResolver.QUERY_ARG_LIMIT, limit)
                            putInt(ContentResolver.QUERY_ARG_OFFSET, offset)
                            // Sort function
                            putString(
                                ContentResolver.QUERY_ARG_SORT_COLUMNS,
                                MediaStore.Files.FileColumns.DATE_MODIFIED
                            )
                            putInt(
                                ContentResolver.QUERY_ARG_SORT_DIRECTION,
                                ContentResolver.QUERY_SORT_DIRECTION_DESCENDING
                            )
                            // Selection
                            putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection)
                            putStringArray(
                                ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS,
                                selectionArgs
                            )
                        }, null
                    )
                } else {
                    val sortOrder =
                        "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC LIMIT $limit OFFSET $offset"
                    // Get All data in Cursor by sorting in DESC order
                    context.contentResolver.query(
                        contentUri(),
                        projection,
                        selection,
                        selectionArgs,
                        sortOrder
                    )
                }?.use { cursor ->
                    while (cursor.moveToNext()) {
                        galleryImageUrls.add(
                            MediaItem(
                                cursor.getLong(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)),
                                ContentUris.withAppendedId(
                                    contentUri(),
                                    cursor.getLong(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID))
                                ),
                                cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA)),
                                cursor.getStringOrNull(cursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE)),
                                cursor.getLongOrNull(cursor.getColumnIndex(MediaStore.Video.Media.DURATION))
                            )
                        )
                    }
                }
            }
        } catch (ex: Exception) {
            ex.printStackTrace()
        }
        return galleryImageUrls
    }
    

提交回复
热议问题