๐ Building an Image Compression Library in Android (Kotlin) Part-1
In Part-1, we will see how to create library for Image compress and use locally in Android Studio. In Part-2 will discuss how to pulishโฆ
๐ Building an Image Compression Library in Android (Kotlin) Part-1

In Part-1, we will see how to create library for Image compress and use locally in Android Studio. In Part-2 will discuss how to pulish library.
Uploading large images in Android apps can lead to slow uploads, high data usage, and failed server requests. To tackle this, we can build a reusable Kotlin library that compresses images before uploading them to a server or Firebase.
In this article, youโll learn:
- Why image compression is important
- How to create a reusable Kotlin image compressor library
- Code snippets for different compression needs
- Code explanations
- How to use it in your app
๐ฆ Why Compress Images in Android?
- ๐ Faster uploads: Reduce image file size.
- ๐ฑ Better user experience: Prevent upload failures.
- โ๏ธ Save bandwidth and storage.
- ๐ Efficient sync with Firebase or backend APIs.
๐ ๏ธ Step 1: Create a Kotlin Library Module
Go to Android Studio:
- File > New > New Module > Android Library
- Name it
imagecompressor
๐ Step 2: Add Compressor Code
In imagecompressor module, create a Kotlin file: ImageCompressor.kt
package com.imagecompressor
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.util.UUID
object ImageCompressor {
fun compressImage(context: Context, imageUri: Uri, quality: Int = 80): File? {
return try {
val inputStream: InputStream? = context.contentResolver.openInputStream(imageUri)
val bitmap = BitmapFactory.decodeStream(inputStream)
inputStream?.close()
val file = File(context.cacheDir, "compressed_${UUID.randomUUID()}.jpg")
val outputStream = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
outputStream.flush()
outputStream.close()
file
} catch (e: Exception) {
e.printStackTrace()
null
}
}
fun compressBitmap(context: Context, bitmap: Bitmap, quality: Int = 80): File? {
return try {
val file = File(context.cacheDir, "compressed_${UUID.randomUUID()}.jpg")
val outputStream = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
outputStream.flush()
outputStream.close()
file
} catch (e: Exception) {
e.printStackTrace()
null
}
}
fun compressToBase64(bitmap: Bitmap, quality: Int = 80): String {
val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
val byteArray = outputStream.toByteArray()
return android.util.Base64.encodeToString(byteArray, android.util.Base64.NO_WRAP)
}
}
โก Code Explanation
compressImage: Takes aUri, converts it to aBitmap, compresses it to JPEG with the given quality, and saves it as a temporary file.compressBitmap: Accepts aBitmapdirectly, compresses and saves it as a temporary file.compressToBase64: Compresses aBitmapto JPEG and returns the base64-encoded string (useful for APIs that accept image strings).
Each method uses context.cacheDir to save the temporary files, which makes them safe and easy to clean up.
๐ Step 3: Use the Library in Your App
Add the module as a dependency in your appโs build.gradle:
implementation project(":imagecompressor")
In your Activity or ViewModel:
val imageUri: Uri = selectedImageUri
val compressedFile = ImageCompressor.compressImage(context, imageUri)
// Use compressedFile with Firebase or upload API
๐ฅ Bonus: Convert Bitmap to Base64 for API Upload
val base64String = ImageCompressor.compressToBase64(bitmap)
// Send base64String in request body
โ๏ธ Firebase Example: Upload Compressed Image
val fileUri = Uri.fromFile(compressedFile)
val ref = FirebaseStorage.getInstance().reference.child("images/${fileUri.lastPathSegment}")
ref.putFile(fileUri)
.addOnSuccessListener { /* success */ }
.addOnFailureListener { /* error */ }
โ Best Practices
- Compress before upload to save bandwidth.
- Use 70โ80% JPEG quality for balance.
- Cache compressed images temporarily.
- Clean up unused temp files.
๐ Summary
Creating your own image compression library in Kotlin helps you build scalable, reusable, and optimized Android apps. Whether youโre using Firebase or a custom backend, pre-processing image uploads can save costs and improve performanc.
Stay tuned for Part-2, where I will explain how to publish Android library.
๋ฉํ๋ฐ์ดํฐ
- post_id
- 4a0bfb605951
- slug
- building-an-image-compression-library-in-android-kotlin-part-1-4a0bfb605951
- url
- https://medium.com/@prahaladsharma4u/building-an-image-compression-library-in-android-kotlin-part-1-4a0bfb605951
- canonical_url
- https://medium.com/@prahaladsharma4u/building-an-image-compression-library-in-android-kotlin-part-1-4a0bfb605951
- author_url
- https://medium.com/@prahaladsharma4u
- status
- ok
- fetched_at
- 2026-07-09 23:43:16