2

I am fetching all the images from gallery and showing it in my android app.

Initialised in onCreate: supportLoaderManager.initLoader(IMAGE_LOADER_ID, null, this)

1st Problem:

In the above initialisation, supportLoaderManager is deprecated now. So what is the alternative?

Secondly, I am using below code to fetch the images:

override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> {

    val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    val projection =
        arrayOf(MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME)
    val selection: String? = null     //Selection criteria
    val selectionArgs = arrayOf<String>()  //Selection criteria
    val sortOrder: String? = MediaStore.Images.Media.DEFAULT_SORT_ORDER

    return CursorLoader(
        applicationContext,
        uri,
        projection,
        selection,
        selectionArgs,
        sortOrder
    )
}

Here the images are coming in random order. So can anyone help me in sorting the images?

1 Answers1

0

After working a bit, I replaced:

val sortOrder: String? = MediaStore.Images.Media.DEFAULT_SORT_ORDER

with:

val sortOrder: String? = MediaStore.Images.Media.DATE_MODIFIED

And it worked fine. Moreover, to add the items in reverse order, I added the elements at 0 position, like:

while (it.moveToNext()) {
    listOfAllImages.add(0, it.getString(columnIndexData))

    binding.RVGalleryImages.layoutManager =
        LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)

    binding.RVGalleryImages.adapter =
        this.let { CreateFeedGalleryAdapter(it, listOfAllImages) }
}

The only issue I am facing is that this code is deprecated and I need a new method to achieve this task.

Dharman
  • 30,962
  • 25
  • 85
  • 135