Skip to content

Commit

Permalink
RecorderThread: Open output files with O_TRUNC
Browse files Browse the repository at this point in the history
The output files should generally be unique, but that's not guaranteed.
Without truncation, the output files may potentially be corrupted due to
the presence of old data at the end.

Issue: #143

Signed-off-by: Andrew Gunnerson <chillermillerlong@hotmail.com>
  • Loading branch information
chenxiaolong committed Oct 7, 2022
1 parent 1feb4e2 commit 4983520
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions app/src/main/java/com/chiller3/bcr/RecorderThread.kt
Expand Up @@ -172,7 +172,7 @@ class RecorderThread(
resultUri = outputFile.uri

try {
openFile(outputFile).use {
openFile(outputFile, true).use {
recordUntilCancelled(it)
}
} finally {
Expand Down Expand Up @@ -404,9 +404,11 @@ class RecorderThread(
*
* @throws IOException if [file] cannot be opened
*/
private fun openFile(file: DocumentFile): ParcelFileDescriptor =
context.contentResolver.openFileDescriptor(file.uri, "rw")
private fun openFile(file: DocumentFile, truncate: Boolean): ParcelFileDescriptor {
val truncParam = if (truncate) { "t" } else { "" }
return context.contentResolver.openFileDescriptor(file.uri, "rw$truncParam")
?: throw IOException("Failed to open file at ${file.uri}")
}

/**
* Record from [MediaRecorder.AudioSource.VOICE_CALL] until [cancel] is called or an audio
Expand Down

0 comments on commit 4983520

Please sign in to comment.