How to get images from phone on API below 29

Viewed 547

I develop image gallery app for Android (min sdk 23, target sdk 29). And I trying to get list of photos from the phone. Here the code:

val list: MutableList<Photo> = mutableListOf()
val projection = arrayOf(
    MediaStore.Images.Media._ID,
    MediaStore.Images.Media.DATE_TAKEN,
    MediaStore.Images.Media.ORIENTATION,
    MediaStore.Images.Media.SIZE
)

val query = contentResolver?.query(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    projection,
    null,
    null,
    MediaStore.Images.Media.DATE_TAKEN + " DESC"
)
query?.use { cursor ->
    if (cursor.count != 0) {
        val idIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID)
        val sizeIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE)
        while (cursor.moveToNext()) {
            val id = cursor.getLong(idIndex)
            val size = cursor.getInt(sizeIndex)

            val contentUri: Uri = ContentUris.withAppendedId(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                id
            )
            val photo = Photo(contentUri, size)
            list.add(photo)
        }
    }
}

But Android Studio says to me that some MediaStore fields requires API level 29+. (e.g. MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.ORIENTATION, MediaStore.Images.Media.DATA, etc.). Screen from AS. So my question is how do I should get images list on APIs below 29? Also studio says same about contentResolver.loadThumbnail() method.

And adjacent question: is it possible in Android to retrieve list of photo albums? Or I should get list of all images and then run over it and pick out albums?

1 Answers

Try this, it's work even with API 29 :

private fun getAllImages(activity: Activity): ArrayList<Photo>? {
    var cursor: Cursor? = null
    var i = 0
    val arrayList: ArrayList<Photo> = ArrayList()
    if (cursor == null) {
        val resolver = activity.contentResolver
        cursor = resolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null,
            null,
            null,
            null
        )
        if (cursor != null) {
            while (i < cursor.count) {
                cursor.moveToPosition(i)
                val fieldIndex: Int = cursor.getColumnIndex(MediaStore.Images.Media._ID)
                val id: Long = cursor.getLong(fieldIndex)
                val imageUri =
                    ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id)
                arrayList.add(Photo("", imageUri))
                i++
            }
            cursor.close()
        }
    }
    return arrayList
}


data class Photo(var name: String, var uri: Uri)

Use any image loader library such as Glide or Picasso :

Glide.with(this).load(getAllImages(this)!![0].uri).into(imgView)

Add this permission to your manifest, and make sure it granted :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Related