Firelands128 / photo_gallery

A Flutter plugin that retrieves images and videos from mobile native gallery.
BSD 3-Clause "New" or "Revised" License
68 stars 62 forks source link

why I can't get newest photo ,after I pick camera from phone? #41

Closed ahmadvicky closed 2 years ago

ahmadvicky commented 2 years ago

Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 2.8.1, on macOS 12.0.1 21A559 darwin-x64, locale en-SG) [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2020.3) [✓] Connected device (3 available)

• No issues found!

what wrong with that's?

hhirpara26 commented 2 years ago

if you're facing this issue in android 11 and above then replace below in listImages() method with existing one

    val media = mutableListOf<Map<String, Any?>>()
    val offset = skip ?: 0
    val limit = take ?: (total - offset)

    this.context?.run {
        val imageCursor: Cursor?

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
            imageCursor = this.contentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                imageMetadataProjection,
                android.os.Bundle().apply {
                    putInt(android.content.ContentResolver.QUERY_ARG_LIMIT, limit)
                    putInt(android.content.ContentResolver.QUERY_ARG_OFFSET, offset)
                    putStringArray(
                        android.content.ContentResolver.QUERY_ARG_SORT_COLUMNS,
                        arrayOf(
                            MediaStore.Images.Media.DATE_ADDED
                        )
                    )
                    putInt(
                        android.content.ContentResolver.QUERY_ARG_SORT_DIRECTION,
                        if (newest) {
                            android.content.ContentResolver.QUERY_SORT_DIRECTION_DESCENDING
                        } else {
                            android.content.ContentResolver.QUERY_SORT_DIRECTION_ASCENDING
                        }
                    )
                    if (albumId != allAlbumId) {
                        putString(
                            android.content.ContentResolver.QUERY_ARG_SQL_SELECTION,
                            "${MediaStore.Images.Media.BUCKET_ID} = ?"
                        )
                        putStringArray(
                            android.content.ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS,
                            arrayOf(albumId)
                        )
                    }
                },
                null
            )
        } else {
            val orderBy = if (newest) {
                "${MediaStore.Images.Media.DATE_TAKEN} DESC, ${MediaStore.Images.Media.DATE_MODIFIED} DESC"
            } else {
                "${MediaStore.Images.Media.DATE_TAKEN} ASC, ${MediaStore.Images.Media.DATE_MODIFIED} ASC"
            }
            imageCursor = this.contentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                imageMetadataProjection,
                if (albumId == allAlbumId) null else "${MediaStore.Images.Media.BUCKET_ID} = $albumId",
                null,
                "$orderBy LIMIT $limit OFFSET $offset"
            )
        }

        imageCursor?.use { cursor ->
            while (cursor.moveToNext()) {
                media.add(getImageMetadata(cursor))
            }
        }
    }

    return mapOf(
        "newest" to newest,
        "start" to offset,
        "total" to total,
        "items" to media
    )