繁体   English   中英

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

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

我正在尝试将图像发布到特定文件夹中。 我已经成功地做到了这一点。 但是为了获取数据,我一直在使用MediaStore 图像 图像列 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()
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM