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

Sharing is caring!

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?
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
AD_LB

This doesn’t seem to work when I try to get album art images of audio files, though:

contentResolver.query(MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL), null, null, null, null)?.use { cursor ->
val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
while (cursor.moveToNext()) {
    val id = cursor.getLong(idColumn)
val contentUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id)
val albumImage = contentResolver.loadThumbnail(contentUri, Size(480, 480), null)
Log.d("AppLog", "hasImage?${albumImage != null} image size: ${albumImage?.width}x${albumImage?.height}")

}

How come (and I've granted it storage permission)?
It keeps getting me exception of FileNotFoundException
Marcos Narvaez Aldana

Great content!! Thanks man!

arjen

Could you show us how to get all images to jetpack compose layout?

Ultron5

Realy thanks! I’m tired of searching and figuring out how it works

Peter Luo

Great! Very helpful!

Scroll to top
6
0
Would love your thoughts, please comment.x
()
x