kotlin 无法选择/通过选定的URI的画廊视频在Android 12和13,无法在选定的屏幕中查看

oxf4rvwz  于 11个月前  发布在  Kotlin
关注(0)|答案(1)|浏览(111)

我无法导航到selectedVideo屏幕,也无法在屏幕中显示视频。

val intent = Intent(Intent.ACTION_PICK, null)
                    intent.type = "video/*"
                    resultLauncher.launch(intent)

 private val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == RESULT_OK) {
            val data: Intent? = result.data

            // For null safety, make sure data is not null
            if (data == null) {
                Log.d("TAG", "Data returned is null")
                return@registerForActivityResult
            }

            val videoUri: Uri = data?.data!!
            val videoPath = parsePath(videoUri)
            if (videoPath != null) {
                Log.d("videoPath", videoPath)
                val intent = Intent(requireActivity(), SelectVideoActivity::class.java)
                intent.putExtra("path", videoUri.path)
                startActivity(intent)
                Log.i("selectedMediaList", videoUri.isAbsolute.toString())
}
}
}

字符串
错误日志:

java.lang.SecurityException: UID 10170 does not have permission to content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F25/ORIGINAL/NONE/video%2Fmp4/1574077794 [user 0] at android.os.Parcel.createExceptionOrNull

qc6wkl3g

qc6wkl3g1#

替换:

val intent = Intent(requireActivity(), SelectVideoActivity::class.java)
                intent.putExtra("path", videoUri.path)
                startActivity(intent)

字符串
使用:

val intent = Intent(requireActivity(), SelectVideoActivity::class.java)
                intent.setData(videoUri).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                startActivity(intent)


SelectVideoActivity中,对Intent使用getData()来检索Uri
this blog post of mine更多
更好的方法是只使用一个Activity,并在单独的屏幕上使用片段或组合。

相关问题