Search code examples
androidvideo-capturemiui

Video from ACTION_VIDEO_CAPTURE return null video after play


I send intent for recording video in this way

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

After I that record video and play it in Mi Video, press back and confirm video file. And try to get video in onActivityResult but there are null file. Intent contains uri like this content:///external_files/..... FileProvider path is 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)
            }
        }
    }

If I record video and confirm it without playing all fine. What can be wrong? I also tried to find video in videoUri path that I put into MediaStore.EXTRA_OUTPUT parameter, no success. Tried to get video path from uri (How to get the Full file path from URI), no success. It reproduced only in MIUI 11, android version 9


Solution

  • Looks like MIUI bug. This issue was resolved after updating MIUI.