Skip to main content
FileKit.compressImage re-encodes an image as JPEG or PNG, optionally downscaled, and returns the new bytes. It is declared in nonWebMain and throws a FileKitException on Linux native, Windows native, and watchOS.

Usage

nonWebMain
Pass bytes = imageBytes instead of file when the image is already in memory.

Options

PlatformFile | ByteArray
required
The source image.
ImageFormat
default:"JPEG"
Output format: ImageFormat.JPEG or ImageFormat.PNG.
Int
default:"80"
0 to 100. Applies to JPEG only.
Int?
default:"null"
Downscales to fit, keeping the aspect ratio. Smaller images are never upscaled.
On Android, the EXIF orientation is applied before resizing, so the output is upright. saveImageToGallery and saveVideoToGallery return a Result<Unit> instead of throwing:
nonWebMain
filename defaults to the file’s name. saveImageToGallery(bytes, filename) saves an image from memory.
On iOS, add NSPhotoLibraryAddUsageDescription to your Info.plist before saving to the gallery. Without it, the system terminates your app on the first save.
On Android 9 and lower, writing to MediaStore requires the WRITE_EXTERNAL_STORAGE permission. FileKit doesn’t declare it, so add it to your manifest with android:maxSdkVersion="28" if you support those versions.

Convert to and from ImageBitmap in Compose

PlatformFile.toImageBitmap() and ImageBitmap.encodeToByteArray() come from filekit-dialogs-compose, not filekit-core. Add it to commonMain (see Installation for the current version):
build.gradle.kts
Both are suspend functions. encodeToByteArray defaults to ImageFormat.JPEG with quality = 100. To display a file in an Image, the Coil loads it for you, with caching.