来自 ACTION_VIDEO_CAPTURE return 空视频的视频播放后
Video from ACTION_VIDEO_CAPTURE return null video after play
我用这种方式发送录制视频的意图
private fun openCamera(url: String) {
if (!viewModel.isAttachmentSaved) {
val takeVideoIntent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
val file = File(url)
val videoURI = if (Build.VERSION_CODES.N <= android.os.Build.VERSION.SDK_INT) {
FileProvider.getUriForFile(
localActivity,
BuildConfig.APPLICATION_ID + ".fileprovider",
file
)
} else {
Uri.fromFile(file)
}
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoURI)
val resInfoList = requireActivity()
.packageManager
.queryIntentActivities(takeVideoIntent, PackageManager.MATCH_DEFAULT_ONLY)
for (resolveInfo in resInfoList) {
val packageName = resolveInfo.activityInfo.packageName
localActivity?.grantUriPermission(
packageName, Uri.parse(url),
Intent.FLAG_GRANT_WRITE_URI_PERMISSION and Intent.FLAG_GRANT_READ_URI_PERMISSION
)
}
takeVideoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION and
Intent.FLAG_GRANT_WRITE_URI_PERMISSION and
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION and
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
startActivityForResult(takeVideoIntent, REQUEST_VIDEO)
}
}
我在小米视频录完视频播放后,按回车确认视频文件。并尝试在 onActivityResult 中获取视频,但有空文件。 Intent 包含这样的 uri content:///external_files/....
。 FileProvider 路径是 external_files
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_VIDEO && resultCode == Activity.RESULT_OK) {
Log.d("bouxView", "onActivityResult $data $requestCode")
//Toast.makeText(localActivity, R.string.text_start_compressing, Toast.LENGTH_LONG).show()
if (isViewModelInitialized()) {
val path = viewModel.videoPathLiveData.value?.run {
substringBeforeLast(FilesRepository.DELIMITER) + "copied" + FilesRepository.DELIMITER + substringAfterLast(FilesRepository.DELIMITER)
}
data?.data?.let {
try {
val fis: InputStream? = context?.contentResolver?.openInputStream(data?.data)
val videoFile = File(path)
val fos = FileOutputStream(videoFile)
val buffer = ByteArray(1024)
var length: Int = 0
var oldLength:Int = 0
while (fis?.read(buffer).also { length = (it ?: 0) } ?: 0 > 0) {
if (oldLength==0){
oldLength = length
}
fos.write(buffer, 0, length)
}
fis?.close()
fos.close()
Toast.makeText(requireContext(),"oldLength = $oldLength, path = $path, data = ${data.data.toString()}, pathLiveData = ${viewModel.videoPathLiveData.value}", Toast.LENGTH_LONG).show()
} catch (e: IOException) {
}
}
viewModel.onVideoSaved(path?:viewModel.videoPathLiveData.value)
}
}
}
如果我录制视频并确认没有播放就没问题。有什么问题?我还尝试在我放入 MediaStore.EXTRA_OUTPUT 参数的 videoUri 路径中查找视频,但没有成功。试图从 uri (How to get the Full file path from URI) 获取视频路径,但没有成功。
仅在 MIUI 11 android 版本 9
中复现
看起来像 MIUI 错误。更新MIUI后解决此问题
我用这种方式发送录制视频的意图
private fun openCamera(url: String) {
if (!viewModel.isAttachmentSaved) {
val takeVideoIntent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
val file = File(url)
val videoURI = if (Build.VERSION_CODES.N <= android.os.Build.VERSION.SDK_INT) {
FileProvider.getUriForFile(
localActivity,
BuildConfig.APPLICATION_ID + ".fileprovider",
file
)
} else {
Uri.fromFile(file)
}
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoURI)
val resInfoList = requireActivity()
.packageManager
.queryIntentActivities(takeVideoIntent, PackageManager.MATCH_DEFAULT_ONLY)
for (resolveInfo in resInfoList) {
val packageName = resolveInfo.activityInfo.packageName
localActivity?.grantUriPermission(
packageName, Uri.parse(url),
Intent.FLAG_GRANT_WRITE_URI_PERMISSION and Intent.FLAG_GRANT_READ_URI_PERMISSION
)
}
takeVideoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION and
Intent.FLAG_GRANT_WRITE_URI_PERMISSION and
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION and
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
startActivityForResult(takeVideoIntent, REQUEST_VIDEO)
}
}
我在小米视频录完视频播放后,按回车确认视频文件。并尝试在 onActivityResult 中获取视频,但有空文件。 Intent 包含这样的 uri content:///external_files/....
。 FileProvider 路径是 external_files
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_VIDEO && resultCode == Activity.RESULT_OK) {
Log.d("bouxView", "onActivityResult $data $requestCode")
//Toast.makeText(localActivity, R.string.text_start_compressing, Toast.LENGTH_LONG).show()
if (isViewModelInitialized()) {
val path = viewModel.videoPathLiveData.value?.run {
substringBeforeLast(FilesRepository.DELIMITER) + "copied" + FilesRepository.DELIMITER + substringAfterLast(FilesRepository.DELIMITER)
}
data?.data?.let {
try {
val fis: InputStream? = context?.contentResolver?.openInputStream(data?.data)
val videoFile = File(path)
val fos = FileOutputStream(videoFile)
val buffer = ByteArray(1024)
var length: Int = 0
var oldLength:Int = 0
while (fis?.read(buffer).also { length = (it ?: 0) } ?: 0 > 0) {
if (oldLength==0){
oldLength = length
}
fos.write(buffer, 0, length)
}
fis?.close()
fos.close()
Toast.makeText(requireContext(),"oldLength = $oldLength, path = $path, data = ${data.data.toString()}, pathLiveData = ${viewModel.videoPathLiveData.value}", Toast.LENGTH_LONG).show()
} catch (e: IOException) {
}
}
viewModel.onVideoSaved(path?:viewModel.videoPathLiveData.value)
}
}
}
如果我录制视频并确认没有播放就没问题。有什么问题?我还尝试在我放入 MediaStore.EXTRA_OUTPUT 参数的 videoUri 路径中查找视频,但没有成功。试图从 uri (How to get the Full file path from URI) 获取视频路径,但没有成功。 仅在 MIUI 11 android 版本 9
中复现看起来像 MIUI 错误。更新MIUI后解决此问题