How to get all the images from the internal or external storage in android 10?

As we can see every new version of android released, Google has made a few changes in terms of security stability. Here I am going to talk about one of the changes has done recently by Google which was related to storing and fetching the images from the internal storage or gallery.

Storing the images in android for all vendor customed OS is very worst designed and save their images based on their framework designed. One example we found when android KitKat has released and while normal query to fetch is the images from external storage will not working because a framework designed has changed in this release.

Let’s see how can we fetch the image from the android storage in the older version of Android 10?

val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI

// DATA is deprecated and might be removed in upcoming Android Versions
val projection = arrayOf(MediaStore.MediaColumns.DATA)

private fun queryStorage() {
        val query = contentResolver.query(
                uri,
                projection,
                null,
                null,
                null
        )
        query.use { cursor ->

            cursor?.let {

                while (cursor.moveToNext()) {
                    val absolutePathOfImage = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
                    val file = File(absolutePathOfImage)))
                }
            }
        }
    }

 

This is a very common code that we have written to get all images from the storage. But the problem is that it will not be working on new android version 10 because the data module is deprecated in the new version.

But again if you want to run the above code in android 10 then you need to add following lines into the android Manifest application tag.

android:requestLegacyExternalStorage="true"

 

However, this change will be temporary and might be not available in the next version release. Then another option is that we need to use the updated API to get access to the images from the internal storage.

In order to fetch the images which are created by my application, we have to make a query on ContentResolver with the INTERNAL_CONTENT_URI. But if you want to get all images that are created by any application then make a query on ContentResolver with EXTERNAL_CONTENT_URI.

As in the query we need to pass the projection object which has the basic information of the images like name, size, date taken and id of the image.

   val imageProjection = arrayOf(
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.SIZE,
                MediaStore.Images.Media.DATE_TAKEN,
                MediaStore.Images.Media._ID
        )

Now we need to pass this projection on query with the help of ContentResolver.

   val cursor = contentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                imageProjection,
                null,
                null,
                null
        )

 

Now we have cursor object which we can use to iterate to get all of the images from this cursor list.

 cursor.use {
            it?.let {
                val idColumn = it.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
                val nameColumn = it.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)
                val sizeColumn = it.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE)
                val dateColumn = it.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN)

                while (it.moveToNext()) {
                    val id = it.getLong(idColumn)
                    val name = it.getString(nameColumn)
                    val size = it.getString(sizeColumn)
                    val date = it.getString(dateColumn)
                 }
      }
}

 

Let me show all source code together to get a clear picture.

private fun queryImageStorage() {

        val imageProjection = arrayOf(
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.SIZE,
                MediaStore.Images.Media.DATE_TAKEN,
                MediaStore.Images.Media._ID
        )

        val imageSortOrder = "${MediaStore.Images.Media.DATE_TAKEN} DESC"

        val cursor = contentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                imageProjection,
                null,
                null,
                imageSortOrder
        )

        cursor.use {
            it?.let {
                val idColumn = it.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
                val nameColumn = it.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)
                val sizeColumn = it.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE)
                val dateColumn = it.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN)

                while (it.moveToNext()) {
                    val id = it.getLong(idColumn)
                    val name = it.getString(nameColumn)
                    val size = it.getString(sizeColumn)
                    val date = it.getString(dateColumn)

                    val contentUri = ContentUris.withAppendedId(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            id
                    )
                    // add the URI to the list 
                   // generate the thumbnail
                   val thumbnail = (this as Context).contentResolver.loadThumbnail(contentUri, Size(480, 480), null)

                }
            } ?: kotlin.run {
                Log.e("TAG", "Cursor is null!")
            }
        }
    }

That’s it. I hope it will help to get the images from the internal and external storage. One thing does not forget to add to check the run time permission to image access.

If you are wondering to learn Android then Please learn from Android category and wonder to learn Kotlin then Kotlin Category will help you. If you want to learn all the python article, then learn from the python category.

Happy Coding 🙂

 

3.4 9 votes
Article Rating
How to get all the images from the internal or external storage in android 10?

Recent Posts

Hide your production API key or any sensitive data in Android

Hi everyone, In this article, we are going to learn how to hide the production… Read More

1 year ago

How to handle the localisation or multi language support in android with examples?

Hello everyone, Today in this article, we are going to learn about localisation to support… Read More

2 years ago

How to convert any callback to Coroutines and use them in Kotlin Android?

Hello everyone, In this article, we are going to learn something to handle the callback… Read More

2 years ago

Request Permission Launcher with Kotlin in Android

In this article, we are learning about the run time permissions for request permission launchers.… Read More

2 years ago

Implement the SMS User Consent API and SMS Retriever API in Android

Hello everyone. In my last tutorial, we learned about the Jetpack Compose introduction and about applying the… Read More

2 years ago

Jetpack Compose Coroutine flow with LiveData/ViewModel in Android

Hello everyone, In this article, we are going to learn about the Jetpack Compose with… Read More

2 years ago