compressFile

fun compressFile(fileToCompress: File, targetSizeBytes: Long = TARGET_SIZE_BYTES): File

Compress an image file so that its encoded size does not exceed targetSizeBytes. The aspect ratio and the EXIF orientation are always preserved.

The original file is returned unchanged when it cannot be decoded as an image, when it is already below the target in a format the server accepts, or when compressing it would not make it any smaller. Otherwise a new file is returned, written in the Sdk cache directory when available; the caller owns it and should delete it once consumed. Note that the returned file may be a JPEG or a PNG regardless of the input format, so its extension may differ from the one of fileToCompress.

Return

A File with the compressed image, or fileToCompress if it was not compressed.

Parameters

fileToCompress

Image file to compress.

targetSizeBytes

Maximum size, in bytes, the resulting file should have.