我正在使用 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/