mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-07-04 18:50:42 +02:00
refactor "from" method
This commit is contained in:
@ -38,6 +38,8 @@ import id.zelory.compressor.constraint.default
|
|||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.io.OutputStream
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
|
|
||||||
class MediaCompressor {
|
class MediaCompressor {
|
||||||
@ -169,7 +171,7 @@ class MediaCompressor {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Log.d("MediaCompressor", "Using image compression $mediaQuality")
|
Log.d("MediaCompressor", "Using image compression $mediaQuality")
|
||||||
val tempFile = from(uri, contentType, context)
|
val tempFile = from(uri, context)
|
||||||
val compressedImageFile =
|
val compressedImageFile =
|
||||||
Compressor.compress(context, tempFile) {
|
Compressor.compress(context, tempFile) {
|
||||||
default(width = 640, format = Bitmap.CompressFormat.JPEG, quality = imageQuality)
|
default(width = 640, format = Bitmap.CompressFormat.JPEG, quality = imageQuality)
|
||||||
@ -185,29 +187,31 @@ class MediaCompressor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun from(
|
private fun from(
|
||||||
uri: Uri?,
|
uri: Uri,
|
||||||
contentType: String?,
|
|
||||||
context: Context,
|
context: Context,
|
||||||
): File {
|
): File {
|
||||||
val extension = contentType?.let { MimeTypeMap.getSingleton().getExtensionFromMimeType(it) } ?: ""
|
val extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(context.contentResolver.getType(uri)) ?: ""
|
||||||
|
val inputStream = context.contentResolver.openInputStream(uri)!!
|
||||||
val inputStream = context.contentResolver.openInputStream(uri!!)
|
val fileName = UUID.randomUUID().toString() + ".$extension"
|
||||||
val fileName: String = 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)
|
||||||
inputStream?.use { input ->
|
|
||||||
FileOutputStream(tempFile).use { output ->
|
copyStream(inputStream, FileOutputStream(tempFile))
|
||||||
|
|
||||||
|
return tempFile
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun copyStream(
|
||||||
|
input: InputStream,
|
||||||
|
output: OutputStream,
|
||||||
|
) {
|
||||||
val buffer = ByteArray(1024 * 50)
|
val buffer = ByteArray(1024 * 50)
|
||||||
var read: Int = input.read(buffer)
|
var read = input.read(buffer)
|
||||||
while (read != -1) {
|
while (read != -1) {
|
||||||
output.write(buffer, 0, read)
|
output.write(buffer, 0, read)
|
||||||
read = input.read(buffer)
|
read = input.read(buffer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return tempFile
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun splitFileName(fileName: String): Pair<String, String> {
|
private fun splitFileName(fileName: String): Pair<String, String> {
|
||||||
val i = fileName.lastIndexOf(".")
|
val i = fileName.lastIndexOf(".")
|
||||||
|
Reference in New Issue
Block a user