0

我以这种方式发送录制视频的意图

  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)
        }
    }

在我录制视频并在 Mi 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 获取视频路径(如何从 URI 获取完整文件路径),没有成功。仅在 MIUI 11、android 版本 9 中重现

4

1 回答 1

0

看起来像 MIUI 错误。更新 MIUI 后,此问题已解决。

于 2020-05-25T08:33:34.923 回答