0

What is the current best way to build a list of images on an Android device? With so many different API's and so many different ways of doing it, I am confused.

I have read about loading them into a cursor object, Media Store, some just plain iterating through folders (with a filter on file type), but nothing seems to work for me.

I essentially want to create a list of all images (or pdfs for example) on the device.

The one consistency is to get Read permission for the internal memory.

I guess what I am asking is, is there any decent (current) tutorials on looping through all folders on the device and adding them to a list / array. I don't mind doing the research / work, but the only stuff I have found is on SO (and most of it is over 6 years old).

Craig Archer
  • 23
  • 1
  • 8

1 Answers1

1

The documentation you should look for this is here:

https://developer.android.com/training/data-storage/shared/media

        private val projection = arrayOf(
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DISPLAY_NAME)

        private val sortOrder = "${MediaStore.Images.Media.DATE_ADDED} DESC"
        
        applicationContext.contentResolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection,
            null,
            null,
            sortOrder
        )?.use { cursor ->

          val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
            while (cursor.moveToNext()) {
                // Use an ID column from the projection to get
                // a URI representing the media item itself.

 val contentUri: Uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id)

            }
        }

This answer's 3rd article in SO have more specific implementation for images in Gallery with Kotlin Coroutines

https://stackoverflow.com/a/36815451/11982611

Eren Tüfekçi
  • 2,463
  • 3
  • 16
  • 35