Exit early if UNCOMPRESSED is selected
This commit is contained in:
+100
-103
@@ -49,113 +49,111 @@ class MediaCompressor {
|
|||||||
onError: (Int) -> Unit,
|
onError: (Int) -> Unit,
|
||||||
mediaQuality: CompressorQuality,
|
mediaQuality: CompressorQuality,
|
||||||
) {
|
) {
|
||||||
if (mediaQuality != CompressorQuality.UNCOMPRESSED) {
|
// Skip compression if user selected uncompressed
|
||||||
checkNotInMainThread()
|
if (mediaQuality == CompressorQuality.UNCOMPRESSED) {
|
||||||
|
Log.d("MediaCompressor", "UNCOMPRESSED quality selected, skipping compression.")
|
||||||
|
onReady(uri, contentType, null)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (contentType?.startsWith("video", true) == true) {
|
checkNotInMainThread()
|
||||||
val videoQuality =
|
|
||||||
when (mediaQuality) {
|
|
||||||
CompressorQuality.VERY_LOW -> VideoQuality.VERY_LOW
|
|
||||||
CompressorQuality.LOW -> VideoQuality.LOW
|
|
||||||
CompressorQuality.MEDIUM -> VideoQuality.MEDIUM
|
|
||||||
CompressorQuality.HIGH -> VideoQuality.HIGH
|
|
||||||
CompressorQuality.VERY_HIGH -> VideoQuality.VERY_HIGH
|
|
||||||
else -> VideoQuality.MEDIUM // dodgy
|
|
||||||
}
|
|
||||||
Log.d("MediaCompressor", "Using video compression $mediaQuality")
|
|
||||||
VideoCompressor.start(
|
|
||||||
// => This is required
|
|
||||||
context = applicationContext,
|
|
||||||
// => Source can be provided as content uris
|
|
||||||
uris = listOf(uri),
|
|
||||||
isStreamable = false,
|
|
||||||
// THIS STORAGE
|
|
||||||
// sharedStorageConfiguration = SharedStorageConfiguration(
|
|
||||||
// saveAt = SaveLocation.movies, // => default is movies
|
|
||||||
// videoName = "compressed_video" // => required name
|
|
||||||
// ),
|
|
||||||
// OR AND NOT BOTH
|
|
||||||
appSpecificStorageConfiguration = AppSpecificStorageConfiguration(),
|
|
||||||
configureWith =
|
|
||||||
Configuration(
|
|
||||||
quality = videoQuality,
|
|
||||||
// => required name
|
|
||||||
videoNames = listOf(UUID.randomUUID().toString()),
|
|
||||||
),
|
|
||||||
listener =
|
|
||||||
object : CompressionListener {
|
|
||||||
override fun onProgress(
|
|
||||||
index: Int,
|
|
||||||
percent: Float,
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStart(index: Int) {
|
if (contentType?.startsWith("video", true) == true) {
|
||||||
// Compression start
|
val videoQuality =
|
||||||
}
|
when (mediaQuality) {
|
||||||
|
CompressorQuality.VERY_LOW -> VideoQuality.VERY_LOW
|
||||||
override fun onSuccess(
|
CompressorQuality.LOW -> VideoQuality.LOW
|
||||||
index: Int,
|
CompressorQuality.MEDIUM -> VideoQuality.MEDIUM
|
||||||
size: Long,
|
CompressorQuality.HIGH -> VideoQuality.HIGH
|
||||||
path: String?,
|
CompressorQuality.VERY_HIGH -> VideoQuality.VERY_HIGH
|
||||||
) {
|
else -> VideoQuality.MEDIUM // dodgy
|
||||||
if (path != null) {
|
|
||||||
Log.d("MediaCompressor", "Video compression success. Compressed size [$size]")
|
|
||||||
onReady(Uri.fromFile(File(path)), contentType, size)
|
|
||||||
} else {
|
|
||||||
Log.d("MediaCompressor", "Video compression successful, but returned null path")
|
|
||||||
onError(R.string.compression_returned_null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onFailure(
|
|
||||||
index: Int,
|
|
||||||
failureMessage: String,
|
|
||||||
) {
|
|
||||||
// keeps going with original video
|
|
||||||
Log.d("MediaCompressor", "Video compression failed: $failureMessage")
|
|
||||||
onReady(uri, contentType, null)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCancelled(index: Int) {
|
|
||||||
onError(R.string.compression_cancelled)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
} else if (
|
|
||||||
contentType?.startsWith("image", true) == true &&
|
|
||||||
!contentType.contains("gif") &&
|
|
||||||
!contentType.contains("svg")
|
|
||||||
) {
|
|
||||||
val imageQuality =
|
|
||||||
when (mediaQuality) {
|
|
||||||
CompressorQuality.VERY_LOW -> 40
|
|
||||||
CompressorQuality.LOW -> 50
|
|
||||||
CompressorQuality.MEDIUM -> 60
|
|
||||||
CompressorQuality.HIGH -> 80
|
|
||||||
CompressorQuality.VERY_HIGH -> 90
|
|
||||||
else -> 60
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Log.d("MediaCompressor", "Using image compression $mediaQuality")
|
|
||||||
val tempFile = from(uri, contentType, applicationContext)
|
|
||||||
val compressedImageFile =
|
|
||||||
Compressor.compress(applicationContext, tempFile) {
|
|
||||||
default(width = 640, format = Bitmap.CompressFormat.JPEG, quality = imageQuality)
|
|
||||||
}
|
|
||||||
Log.d("MediaCompressor", "Image compression success. Original size [${tempFile.length()}], new size [${compressedImageFile.length()}]")
|
|
||||||
onReady(compressedImageFile.toUri(), contentType, compressedImageFile.length())
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.d("MediaCompressor", "Image compression failed: ${e.message}")
|
|
||||||
if (e is CancellationException) throw e
|
|
||||||
e.printStackTrace()
|
|
||||||
onReady(uri, contentType, null)
|
|
||||||
}
|
}
|
||||||
} else {
|
Log.d("MediaCompressor", "Using video compression $mediaQuality")
|
||||||
|
VideoCompressor.start(
|
||||||
|
// => This is required
|
||||||
|
context = applicationContext,
|
||||||
|
// => Source can be provided as content uris
|
||||||
|
uris = listOf(uri),
|
||||||
|
isStreamable = false,
|
||||||
|
// THIS STORAGE
|
||||||
|
// sharedStorageConfiguration = SharedStorageConfiguration(
|
||||||
|
// saveAt = SaveLocation.movies, // => default is movies
|
||||||
|
// videoName = "compressed_video" // => required name
|
||||||
|
// ),
|
||||||
|
// OR AND NOT BOTH
|
||||||
|
appSpecificStorageConfiguration = AppSpecificStorageConfiguration(),
|
||||||
|
configureWith =
|
||||||
|
Configuration(
|
||||||
|
quality = videoQuality,
|
||||||
|
// => required name
|
||||||
|
videoNames = listOf(UUID.randomUUID().toString()),
|
||||||
|
),
|
||||||
|
listener =
|
||||||
|
object : CompressionListener {
|
||||||
|
override fun onProgress(
|
||||||
|
index: Int,
|
||||||
|
percent: Float,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onStart(index: Int) {
|
||||||
|
// Compression start
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSuccess(
|
||||||
|
index: Int,
|
||||||
|
size: Long,
|
||||||
|
path: String?,
|
||||||
|
) {
|
||||||
|
if (path != null) {
|
||||||
|
Log.d("MediaCompressor", "Video compression success. Compressed size [$size]")
|
||||||
|
onReady(Uri.fromFile(File(path)), contentType, size)
|
||||||
|
} else {
|
||||||
|
Log.d("MediaCompressor", "Video compression successful, but returned null path")
|
||||||
|
onError(R.string.compression_returned_null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFailure(
|
||||||
|
index: Int,
|
||||||
|
failureMessage: String,
|
||||||
|
) {
|
||||||
|
// keeps going with original video
|
||||||
|
Log.d("MediaCompressor", "Video compression failed: $failureMessage")
|
||||||
|
onReady(uri, contentType, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCancelled(index: Int) {
|
||||||
|
onError(R.string.compression_cancelled)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
} else if (contentType?.startsWith("image", true) == true && !contentType.contains("gif") && !contentType.contains("svg")) {
|
||||||
|
val imageQuality =
|
||||||
|
when (mediaQuality) {
|
||||||
|
CompressorQuality.VERY_LOW -> 40
|
||||||
|
CompressorQuality.LOW -> 50
|
||||||
|
CompressorQuality.MEDIUM -> 60
|
||||||
|
CompressorQuality.HIGH -> 80
|
||||||
|
CompressorQuality.VERY_HIGH -> 90
|
||||||
|
else -> 60
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Log.d("MediaCompressor", "Using image compression $mediaQuality")
|
||||||
|
val tempFile = from(uri, contentType, applicationContext)
|
||||||
|
val compressedImageFile =
|
||||||
|
Compressor.compress(applicationContext, tempFile) {
|
||||||
|
default(width = 640, format = Bitmap.CompressFormat.JPEG, quality = imageQuality)
|
||||||
|
}
|
||||||
|
Log.d("MediaCompressor", "Image compression success. Original size [${tempFile.length()}], new size [${compressedImageFile.length()}]")
|
||||||
|
onReady(compressedImageFile.toUri(), contentType, compressedImageFile.length())
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.d("MediaCompressor", "Image compression failed: ${e.message}")
|
||||||
|
if (e is CancellationException) throw e
|
||||||
|
e.printStackTrace()
|
||||||
onReady(uri, contentType, null)
|
onReady(uri, contentType, null)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.d("MediaCompressor", "UNCOMPRESSED quality selected, skip compression and continue with original media.")
|
|
||||||
onReady(uri, contentType, null)
|
onReady(uri, contentType, null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -165,8 +163,7 @@ class MediaCompressor {
|
|||||||
contentType: String?,
|
contentType: String?,
|
||||||
context: Context,
|
context: Context,
|
||||||
): File {
|
): File {
|
||||||
val extension =
|
val extension = contentType?.let { MimeTypeMap.getSingleton().getExtensionFromMimeType(it) } ?: ""
|
||||||
contentType?.let { MimeTypeMap.getSingleton().getExtensionFromMimeType(it) } ?: ""
|
|
||||||
|
|
||||||
val inputStream = context.contentResolver.openInputStream(uri!!)
|
val inputStream = context.contentResolver.openInputStream(uri!!)
|
||||||
val fileName: String = UUID.randomUUID().toString() + ".$extension"
|
val fileName: String = UUID.randomUUID().toString() + ".$extension"
|
||||||
|
|||||||
Reference in New Issue
Block a user