'MediaStore.MediaColumns.DATA'已被弃用,我应该使用哪一列作为替代
我想使用 API 将所有图片从媒体提供程序加载到我的应用,但此列已弃用。那么,我可以使用什么作为替代方案呢?MediaStore
MediaStore.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