Skip to content

Commit

Permalink
Use "use" blocks to close resources automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
davotoula committed Oct 8, 2024
1 parent af28e6d commit fbecc8b
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ object MediaCompressorFileUtils {
context: Context,
): File {
val extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(context.contentResolver.getType(uri)) ?: ""
val inputStream = context.contentResolver.openInputStream(uri)!!
val fileName = UUID.randomUUID().toString() + ".$extension"
val (name, ext) = splitFileName(fileName)
val tempFile = File.createTempFile(name, ext)

copyStream(inputStream, FileOutputStream(tempFile))
context.contentResolver.openInputStream(uri)?.use { inputStream ->
FileOutputStream(tempFile).use { outputStream ->
copyStream(inputStream, outputStream)
}
}

return tempFile
}
Expand Down

0 comments on commit fbecc8b

Please sign in to comment.