我想使用MediaStoreAPI 将媒体提供商的所有图片加载到我的应用程序,但此列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
慕尼黑8549860
喵喵时光机
PIPIONE
繁星点点滴滴
凤凰求蛊
相关分类