Use "use" blocks to close resources automatically
This commit is contained in:
+5
-2
@@ -35,12 +35,15 @@ object MediaCompressorFileUtils {
|
|||||||
context: Context,
|
context: Context,
|
||||||
): File {
|
): File {
|
||||||
val extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(context.contentResolver.getType(uri)) ?: ""
|
val extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(context.contentResolver.getType(uri)) ?: ""
|
||||||
val inputStream = context.contentResolver.openInputStream(uri)!!
|
|
||||||
val fileName = UUID.randomUUID().toString() + ".$extension"
|
val fileName = UUID.randomUUID().toString() + ".$extension"
|
||||||
val (name, ext) = splitFileName(fileName)
|
val (name, ext) = splitFileName(fileName)
|
||||||
val tempFile = File.createTempFile(name, ext)
|
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
|
return tempFile
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user