Search code examples
androidandroid-filemediastorescoped-storage

How to get Image Data using MediaStore from a specific folder post deprecation of MediaStore.Images.ImageColumns.DATA


I am trying to get images post setting them in a specific folder . I have been successful doing that . But for the sake of getting data I have been using MediaStore.Images.ImageColumns.DATA which is deprecated and thus I want to use newer method to retrieve information .

I have saved the image using the following code and works completely fine


suspend fun saveImage(context: Context, bitmap: Bitmap) {
        withContext(Dispatchers.IO) {
            val collection =
                MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
            val dirDest = File(
                Environment.DIRECTORY_PICTURES,
                context.getString(R.string.app_name) + File.separator + CAMERA
            )
            val filePath = dirDest.absolutePath
            val date = System.currentTimeMillis()

            val newImage = ContentValues().apply {
                put(MediaStore.Images.Media.DISPLAY_NAME, "$date.$IMAGE_EXTENSIONS")
                put(MediaStore.MediaColumns.MIME_TYPE, "image/$IMAGE_EXTENSIONS")
                put(MediaStore.MediaColumns.DATE_ADDED, date)
                put(MediaStore.MediaColumns.DATE_MODIFIED, date)
                put(MediaStore.MediaColumns.SIZE, bitmap.byteCount)
                put(MediaStore.MediaColumns.WIDTH, bitmap.width)
                put(MediaStore.MediaColumns.HEIGHT, bitmap.height)
                put(MediaStore.MediaColumns.RELATIVE_PATH, "$dirDest${File.separator}")
                put(MediaStore.Images.Media.IS_PENDING, 1)
            }


            val newImageUri = context.contentResolver.insert(collection, newImage)

            context.contentResolver.openOutputStream(newImageUri!!, "w").use {
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it)
            }

            newImage.clear()
            newImage.put(MediaStore.Images.Media.IS_PENDING, 0)
            context.contentResolver.update(newImageUri, newImage, null, null)
        }
    }

The issue is while retreiving the information . The code used by me is as follows :

  private fun getData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val collection = sdk29AndUp {
                MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
            } ?: MediaStore.Images.Media.EXTERNAL_CONTENT_URI


            val file = File(
                Environment.DIRECTORY_PICTURES,
                requireContext().getString(R.string.app_name) + File.separator + CAMERA
            )


            val filePath = file.absolutePath

           val selection = MediaStore.Images.ImageColumns.DATA + " like '%" + filePath + "%'"

            val projection = arrayOf(
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DISPLAY_NAME
            )
            val photoList = mutableListOf<SharedStoragePhoto>()

            contentResolver?.query(
                collection,
                projection,
                selection,
                null,
                null
            )?.use { cursor ->

                val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
                val displayNameColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)
                while (cursor.moveToNext()) {
                    val id = cursor.getLong(idColumn)
                    val displayName = cursor.getString(displayNameColumn)

                    photoList.add(SharedStoragePhoto(id, displayName))

                }

            }
           
        }
    }

Since using MediaStore.Images.ImageColumns.DATA is deprecated please suggest workaround for it. Thanking in advance


Solution

  • As answerd by @blackapps. This is the way to solve the problem using RELATIVE_PATH

    
        private fun getData() {
    
            lifecycleScope.launch(Dispatchers.IO) {
                val collection = sdk29AndUp {
                    MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
                } ?: MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    
                val selection =
                    MediaStore.Images.ImageColumns.RELATIVE_PATH + " like '%" + CAMERA + "%'"
                val projection = arrayOf(
                    MediaStore.Images.Media._ID,
                    MediaStore.Images.Media.DISPLAY_NAME,
                    MediaStore.Images.Media.RELATIVE_PATH
                )
                contentResolver?.query(
                    collection,
                    projection,
                    selection,
                    null,
                    null
                )?.use { cursor ->
    
                    val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
                    val displayNameColumn =
                        cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)
                    val relativePathColumn =
                        cursor.getColumnIndexOrThrow(MediaStore.Images.Media.RELATIVE_PATH)
                    while (cursor.moveToNext()) {
                        val id = cursor.getLong(idColumn)
                        val displayName = cursor.getString(displayNameColumn)
                        val relativePath = cursor.getString(relativePathColumn)
    
                        val contentUri = ContentUris.withAppendedId(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            id
                        )
                            )
                        }
                    }
    
                    cursor.close()
                }
            }
        }