'MediaStore.MediaColumns.DATA'已被弃用,我应该使用哪一列作为替代

2022-09-01 21:01:02

我想使用 API 将所有图片从媒体提供程序加载到我的应用,但此列已弃用。那么,我可以使用什么作为替代方案呢?MediaStoreMediaStore.MediaColumns.DATA

我想更新以下代码以摆脱已弃用的列:MediaStore.MediaColumns.DATA

fun ContentResolver.loadImagesPaths(): List<String> {
    val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    val images = mutableListOf<String>()
    val projection = arrayOf(MediaStore.MediaColumns.DATA)
    query(uri, projection, null, null, null)?.use { cursor ->
        val dataColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)
        while (cursor.moveToNext()) {
            images += cursor.getString(dataColumn)
        }
    }
    return images
}

编辑:

更新代码:

fun ContentResolver.loadImagesUris(): List<Uri> {
    val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    val images = mutableListOf<Uri>()
    val projection = arrayOf(MediaStore.Images.Media._ID)
    query(uri, projection, null, null, null)?.use { cursor ->
        val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
        while (cursor.moveToNext()) {
            images += ContentUris.withAppendedId(uri, cursor.getLong(idColumn))
        }
    }
    return images
}

查看此链接,了解有关如何从共享存储访问媒体文件的更多详细信息:https://developer.android.com/training/data-storage/shared/media


答案 1

我能够将MediaStore.MediaColumns.Data替换为自己的文件ID(令人难以置信的是,文件具有ID)并正确构造其URI,如下所示:

fun getAllShownImagesPath(activity: Activity): MutableList<Uri> {
    val uriExternal: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    val cursor: Cursor?
    val columnIndexID: Int
    val listOfAllImages: MutableList<Uri> = mutableListOf()
    val projection = arrayOf(MediaStore.Images.Media._ID)
    var imageId: Long
    cursor = activity.contentResolver.query(uriExternal, projection, null, null, null)
    if (cursor != null) {
        columnIndexID = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
        while (cursor.moveToNext()) {
            imageId = cursor.getLong(columnIndexID)
            val uriImage = Uri.withAppendedPath(uriExternal, "" + imageId)
            listOfAllImages.add(uriImage)
        }
        cursor.close()
    }
    return listOfAllImages
}

然后使用Uri,您可以在视图中构建它!


答案 2

我设法提出了以下解决方案,这是对先前答案的补充

但在那里,我仍然无法使用获得的Uri加载图像。文档建议使用openFileDescriptor(),我做了,然后从中解码了图像的位图:

override fun loadImagesFromStorage(): List<AdapterImage> {

    val uri: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    val cursor: Cursor?
    val columnIndexId: Int
    val listOfAllImages = mutableListOf<AdapterImage>()
    val projection = arrayOf(MediaStore.Images.Media._ID)
    cursor = context.contentResolver
        .query( uri, projection, null, null, null)

    if ( cursor != null ){
        columnIndexId = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
        while (cursor.moveToNext()){

            val contentUri = ContentUris.withAppendedId(uri, cursor.getLong(columnIndexId))

            //here I open FileDescriptor and then decode it into Bitmap
            var image: Bitmap
            context.contentResolver.openFileDescriptor(contentUri, "r").use { pfd ->
                if( pfd != null ){
                    image = BitmapFactory.decodeFileDescriptor(pfd.fileDescriptor)
                    listOfAllImages.add(AdapterImage(image))
                }
            }

        }
        cursor.close()
    }

    return listOfAllImages
}

附言:我的方法将返回我稍后在应用程序中使用的适配器图像对象列表,但此时您可以将所需的任何内容放在那里


推荐