如何使用 MediaStore 从特定文件夹获取图像数据 post 弃用 MediaStore.Images.ImageColumns.DATA

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

我正在尝试获取图像 post 并将它们设置在特定文件夹中。我已经成功地做到了这一点。但是为了获取数据,我一直在使用 MediaStore.Images.ImageColumns.DATA 已弃用,因此我想使用更新的方法来检索信息。

我已经使用以下代码保存了图像并且工作正常


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)
        }
    }

问题出在检索信息时。 我使用的代码如下:

  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))

                }

            }
           
        }
    }

由于不推荐使用 MediaStore.Images.ImageColumns.DATA,请提出解决方法。 提前致谢

如@blackapps 所回答。这是使用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()
            }
        }
    }