在Android 10/Q中从多媒体资料中选择视频时遇到问题

在Android 10/Q中从多媒体资料中选择视频时遇到问题,android,android-contentprovider,android-gallery,android-10.0,Android,Android Contentprovider,Android Gallery,Android 10.0,在Android 10中,我无法从多媒体资料中挑选视频。我从挑选视频中获得Uri,但我不确定如何在onActivityResult中使用该Uri与内容提供商一起检索用户选择的视频 fun loadVideoFromGallery(): Intent { return Intent( Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI ).apply {

在Android 10中,我无法从多媒体资料中挑选视频。我从挑选视频中获得Uri,但我不确定如何在onActivityResult中使用该Uri与内容提供商一起检索用户选择的视频

    fun loadVideoFromGallery(): Intent {
        return Intent(
            Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI
        ).apply {
            type = "video/*"
        }
    }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (resultCode == Activity.RESULT_OK && null != data) {
        val selectedVideoUri = date?.data
        if (selectedVideoUri != null) {
            val projection = arrayOf(
                        MediaStore.Video.Media._ID
                    )
            val selection: String? = null
            val selectionArgs: Array<String>? = null
            contentResolver.query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                selectionArgs,
                null
            )?.use { cursor ->
                try {
                    val idColumn =
                            cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)
                    while (cursor.moveToNext()) {
                        val id = cursor.getLong(idColumn)
                        val contentUri: Uri = ContentUris.withAppendedId(
                            MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id
                        )
                        val thumb = contentResolver.loadThumbnail(
                            contentUri, Size(imageView.width, imageView.height), null
                        )
                        imageView.setImageBitmap(thumb)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
    }
}
以下是我在onActivityResult(data?.data)中获得的Uri:

content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F28/ORIGINAL/NONE/1128816056

我能在整个过程中循环

while(cursor.moveToNext())

我可以使用我正在循环浏览的内容,但我不知道如何获取用户选择的视频

    fun loadVideoFromGallery(): Intent {
        return Intent(
            Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI
        ).apply {
            type = "video/*"
        }
    }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (resultCode == Activity.RESULT_OK && null != data) {
        val selectedVideoUri = date?.data
        if (selectedVideoUri != null) {
            val projection = arrayOf(
                        MediaStore.Video.Media._ID
                    )
            val selection: String? = null
            val selectionArgs: Array<String>? = null
            contentResolver.query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                selectionArgs,
                null
            )?.use { cursor ->
                try {
                    val idColumn =
                            cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)
                    while (cursor.moveToNext()) {
                        val id = cursor.getLong(idColumn)
                        val contentUri: Uri = ContentUris.withAppendedId(
                            MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id
                        )
                        val thumb = contentResolver.loadThumbnail(
                            contentUri, Size(imageView.width, imageView.height), null
                        )
                        imageView.setImageBitmap(thumb)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
    }
}
override-on-activityresult(请求代码:Int,结果代码:Int,数据:Intent?){
super.onActivityResult(请求代码、结果代码、数据)
if(resultCode==Activity.RESULT\u OK&&null!=数据){
val selectedVideoUri=date?.data
if(selectedVideoUri!=null){
val投影=阵列(
MediaStore.Video.Media.\u ID
)
val选择:字符串?=null
val selectionArgs:数组?=null
contentResolver.query(
MediaStore.Video.Media.EXTERNAL\u CONTENT\u URI,
投影,
选择,
精选,
无效的
)?使用{游标->
试一试{
val ID列=
cursor.getColumnIndexOrThrow(MediaStore.Video.Media.\u ID)
while(cursor.moveToNext()){
val id=cursor.getLong(idColumn)
val contentUri:Uri=ContentUris.withAppendedId(
MediaStore.Video.Media.EXTERNAL\u CONTENT\u URI,id
)
val thumb=contentResolver.loadThumbnail(
contentUri,大小(imageView.width,imageView.height),null
)
设置图像位图(拇指)
}
}捕获(e:例外){
e、 printStackTrace()
}
}
}
}
}
如您所见,我正在MediaStore.Video.Media.EXTERNAL_CONTENT_URI目录中的元素中循环,但imageView.setImageBitmap(thumb)始终是它循环通过的最后一个元素

我已经阅读了文档,但仍然没有找到解决这个问题的方法。任何关于如何与内容提供商交互的帮助都将不胜感激

  imageView.setImageUri(data.getData());