android - 无法在 android 12 和 13 中选择/传递图库视频的选定 URI,并且无法在选定的屏幕中查看

标签 android kotlin video mp4 video-editing

我正在使用 Android 应用程序并尝试将图库视频添加到我的视频编辑屏幕。但无法显示视频并显示黑屏。

任何人都可以让我知道我做错了什么吗?

我点击图库图标并尝试从 ACTION_PICK 打开视频并将该视频发送到下一个 Activity 。

问题:我在 LogCat 中收到此错误:

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 

这是我的代码:

VideoFragment.kt

R.id.imageViewVideo -> {
            if (android.os.Build.VERSION.SDK_INT >= 32) {
                val intent = Intent(Intent.ACTION_PICK, null) // It shows the videos alone
                intent.type = "video/*"
                resultLauncher.launch(intent)
            } else {
            requestPermission()
            }
        }

private val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == RESULT_OK) {
            val data: Intent? = result.data
            val videoUri: Uri = data?.data!!
            Log.i("videoUri", videoUri.toString()) // content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F1000000024/ORIGINAL/NONE/video%2Fmp4/1940625115
            val videoPath = parsePath(videoUri)
            if (videoPath != null) {
                Log.i("videoPath", videoPath) 
 /storage/emulated/0/Movies/VID_20231115_163657.mp4
                Log.i("videoPath22", videoUri.path.toString()) /-1/2/content://media/external/video/media/1000000024/ORIGINAL/NONE/video/mp4/1940625115
            }
           

            val intent = Intent(<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cebaa6a7bd8e8dafa3abbcaffc98a7aaaba188bcafa9a3aba0bae0bcabbfbba7bcab8da1a0baabb6ba" rel="noreferrer noopener nofollow">[email protected]</a>(), SelectVideoActivity::class.java)
            intent.putExtra("path", videoPath)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(intent)
}
}

下一个 Activity 文件:(SelectVideoActivity.kt)

onCreate()

val source = File(intent!!.getStringExtra("path")!!)
                    copyFile(source, inputFile)
                    Log.i("input file ===", inputFile.toString())


@Throws(IOException::class)
    fun copyFile(sourceFile: File?, destFile: File) {
        try {
                if (!destFile.parentFile!!.exists()) destFile.parentFile!!.mkdirs()
                if (!destFile.exists()) {
                    destFile.createNewFile()
                }
                var source: FileChannel? = null
                var destination: FileChannel? = null

                source = FileInputStream(sourceFile).channel
                destination = FileOutputStream(destFile).channel
                destination.transferFrom(source, 0, source.size())
            }
            catch (e:Exception){
                Log.i("error","selectvideo ${e.message}")
            }finally {
//                source?.close()
//                destination?.close()
                initViews()
                setListener()
            }
    }

对于最低版本的 Android,路径如下所示,并且工作正常

文件路径:

/storage/emulated/0/DCIM/Camera/VID_20231121_193407.mp4/storage/emulated/0/Android/data/com.app.packagename/files/app-name/1700718359149.mp4

代码:

private lateinit var inputFile: File

onCreate方法:

     inputFile =
                File(getExternalFilesDir("slow-mow")!!.absolutePath.plus("/" + "${System.currentTimeMillis()}.mp4"))

最佳答案

替换:

                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 ,使用getData()关于Intent检索Uri .

参见this blog post of mine了解更多。

更好的是只有一个 Activity 并为单独的屏幕使用 fragment 或可组合项。

关于android - 无法在 android 12 和 13 中选择/传递图库视频的选定 URI,并且无法在选定的屏幕中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77514967/

相关文章:

android - 修复 android 和 iOS 原生 react 中的行数

java - 如何将 Kotlin 文件添加到旧的 android java 项目?

android - 如何修复无法在没有参数的情况下调用私有(private)的问题

javascript - 使用 HTML5 视频和 Javascript 的带有自定义控件的视频

Android Studio,drawable仅在 Debug模式下可用

android - 通过上行链路通话中发送 DTMF 音

java - colors.xml 资源不起作用

list - Kotlin - 基于对象属性对 MutableList 进行排序

python - 无法在 OpenCV python 中打开视频文件

html - 来自与 youtube 不同的来源的视频进入花式盒