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
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.
Save to the gallery
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 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
encodeToByteArray defaults to ImageFormat.JPEG with quality = 100. To display a file in an Image, the Coil loads it for you, with caching.
Related
- Gallery picker to let users pick photos and videos.
- Camera picker to take a photo.
- Coil to display a
PlatformFileimage.