0
votes

I am passing filepath to MediaRecorder for creating video file.

File(filePath).exists() returns false, but MediaRecorder fails with IOException, java.io.FileNotFoundException: /storage/emulated/0/DCIM/XXX/XXX0001.mp4: open failed: EEXIST (File exists)

I have tried creating Uri by getContentResolver().insert(), but it also gives UNIQUE constraint failed: files._data (code 2067 SQLITE_CONSTRAINT_UNIQUE[2067])

Problem doesn't come in a new phone in which I had never tested my application. Problem starts If I delete video from file Manager.

2

2 Answers

0
votes

Create a temp file and copy whole content in the temp file.

/**
  * Create a temp file with the specified format.
  * Usage: Image from gallery, Copy file to app directory before upload
  */
@SuppressLint("SimpleDateFormat")
    suspend fun createTempFile(fileType: BaseMediaFragment.FileType, extension: String): File? =
        withContext(Dispatchers.IO) {
            val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
            val storageDir: File? =
                getApplication<Application>().applicationContext?.getExternalFilesDir(fileType.tempFileDirectory)

            if (!storageDir?.exists().isTrue())
                storageDir?.mkdirs()

            return@withContext File.createTempFile(
                "${fileType.tempFilePrefix}_${timeStamp}_", /* prefix */
                extension, /* suffix */
                storageDir /* directory */
            )
        }

    /**
     * Copy the specified input stream to the output file.
     */
    private suspend fun copyStreamToFile(inputStream: InputStream, outputFile: File) {
        withContext(Dispatchers.IO) {
            inputStream.use { input ->
                val outputStream = FileOutputStream(outputFile)
                outputStream.use { output ->
                    val buffer = ByteArray(4 * 1024) // buffer size
                    while (true) {
                        val byteCount = input.read(buffer)
                        if (byteCount < 0) break
                        output.write(buffer, 0, byteCount)
                    }
                    output.flush()
                }
            }
        }
    ```
0
votes

You can use this type to set mMediaRecoder's path:

final ParcelFileDescriptor parcelFileDescriptor = mContext.getContentResolver().
             openFileDescriptor(Uri.parse(mVideoProfile.path), "rw");

mMediaRecorder.setOutputFile(parcelFileDescriptor.getFileDescriptor());

mVideoProfile.path = "content://media/external/video/media/751";