Skip to main content

How to compress the selected image using ImagePicker in Kotlin Android

How to compress the selected image using ImagePicker in Kotlin Android.

Here's a step-by-step tutorial on how to compress a selected image using ImagePicker in Kotlin for Android.

Step 1: Set up the project

Start by creating a new Android project in Kotlin. Make sure you have the necessary dependencies in your project's build.gradle file. Add the following dependency for ImagePicker:

implementation 'com.github.dhaval2404:imagepicker-support:1.4'

Sync your project to ensure the dependency is added successfully.

Step 2: Request permission

Before selecting an image, you need to request the necessary permissions from the user. Add the following code to your activity's onCreate method:

val permission = Manifest.permission.READ_EXTERNAL_STORAGE
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(permission), 1)

This code checks if the READ_EXTERNAL_STORAGE permission is granted. If not, it requests the permission from the user.

Step 3: Implement image selection

Now, let's implement the image selection using ImagePicker. Add the following code to your activity:

.start { resultCode, data ->
if (resultCode == Activity.RESULT_OK) {
val fileUri = data?.data
// Compress the selected image

This code sets up ImagePicker to only allow selection from the gallery. It then starts the selection process. When the user selects an image, the onActivityResult method is called with the result code and data. If the result is OK, we get the file URI of the selected image and pass it to the compressImage function.

Step 4: Compress the image

Now, let's implement the compressImage function. Add the following code to your activity:

private fun compressImage(fileUri: Uri?) {
fileUri?.let {
val filePath = ImagePicker.getFilePath(this, fileUri)
val compressedImageFile = Compressor.compress(this, File(filePath)) {
resolution(1280, 720)

// Use the compressedImageFile as needed
// For example, you can display it in an ImageView

This code first gets the file path of the selected image using ImagePicker. Then, it uses the Compressor library to compress the image. In this example, the resolution is set to 1280x720, the quality is set to 80, and the format is set to JPEG. You can adjust these values as per your requirements.

Finally, the compressed image file can be used as needed. In this example, it's being displayed in an ImageView.

Step 5: Handle permissions

To handle the permission request result, add the following code to your activity:

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
if (requestCode == 1 && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, you can now select an image
} else {
// Permission denied, handle accordingly

This code checks if the permission request was for READ_EXTERNAL_STORAGE and if it was granted. If the permission is granted, you can proceed with image selection. If not, you can handle the denial accordingly.

That's it! You have successfully implemented image compression using ImagePicker in Kotlin for Android. You can now select an image from the gallery, compress it, and use it as needed in your app.