Skip to main content

How to resize the selected image using ImagePicker in Kotlin Android

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

Here is a step-by-step tutorial on how to resize a selected image using ImagePicker in Kotlin for Android:

Step 1: Set up the project

  1. Create a new Android project in Kotlin.

  2. Add the necessary dependencies in your app-level build.gradle file:

    implementation 'com.github.dhaval2404:imagepicker:1.7.8'

Step 2: Add permissions and activities to the manifest file

  1. Open your app's manifest file (AndroidManifest.xml).

  2. Add the following permissions to access and read external storage:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  3. Add the following activities to handle image picking:

    <activity
    android:name="com.github.dhaval2404.imagepicker.ImagePickerActivity"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
    <activity
    android:name="com.github.dhaval2404.imagepicker.ImagePreviewActivity"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

Step 3: Implement ImagePicker in your activity

  1. Open your desired activity where you want to implement the image picker.

  2. Create a button or any other UI element to trigger the image selection.

  3. Add an OnClickListener to the button and implement the image picking logic:

    import com.github.dhaval2404.imagepicker.ImagePicker

    // Inside your activity class
    private val PICK_IMAGE_REQUEST = 123

    // Inside your button click listener or any other event
    ImagePicker.with(this)
    .cropSquare() // Optional, for square cropping
    .compress(1024) // Optional, reduces the image size to 1024 KB
    .start(PICK_IMAGE_REQUEST)
  4. Override the onActivityResult method to handle the result of the image picker:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null) {
    val imageUri = data.data
    // Resize the selected image
    resizeImage(imageUri)
    }
    }

Step 4: Resize the selected image

  1. Create a new Kotlin file called ImageUtils.kt.

  2. Add the following code inside the file to resize the selected image:

    import android.content.Context
    import android.graphics.Bitmap
    import android.graphics.Matrix
    import android.net.Uri
    import androidx.exifinterface.media.ExifInterface
    import java.io.IOException

    object ImageUtils {

    fun resizeImage(context: Context, uri: Uri, maxWidth: Int, maxHeight: Int): Bitmap? {
    var bitmap: Bitmap? = null
    try {
    val options = BitmapFactory.Options().apply {
    inJustDecodeBounds = true
    BitmapFactory.decodeStream(context.contentResolver.openInputStream(uri), null, this)
    inSampleSize = calculateInSampleSize(this, maxWidth, maxHeight)
    inJustDecodeBounds = false
    }

    val originalBitmap = BitmapFactory.decodeStream(context.contentResolver.openInputStream(uri), null, options)
    val rotation = getRotation(context, uri)
    bitmap = rotateBitmap(originalBitmap, rotation)
    } catch (e: IOException) {
    e.printStackTrace()
    }
    return bitmap
    }

    private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
    val height = options.outHeight
    val width = options.outWidth
    var inSampleSize = 1

    if (height > reqHeight || width > reqWidth) {
    val halfHeight = height / 2
    val halfWidth = width / 2

    while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
    inSampleSize *= 2
    }
    }
    return inSampleSize
    }

    private fun getRotation(context: Context, uri: Uri): Float {
    val rotation: Float
    val contentResolver = context.contentResolver
    val inputStream = contentResolver.openInputStream(uri)
    val exifInterface = ExifInterface(inputStream!!)
    val orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
    rotation = when (orientation) {
    ExifInterface.ORIENTATION_ROTATE_90 -> 90f
    ExifInterface.ORIENTATION_ROTATE_180 -> 180f
    ExifInterface.ORIENTATION_ROTATE_270 -> 270f
    else -> 0f
    }
    return rotation
    }

    private fun rotateBitmap(bitmap: Bitmap, rotation: Float): Bitmap {
    val matrix = Matrix()
    matrix.postRotate(rotation)
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
    }
    }
  3. Modify the onActivityResult method in your activity to call the resizeImage function:

    private fun resizeImage(uri: Uri) {
    val maxWidth = 800 // Set your desired maximum width
    val maxHeight = 800 // Set your desired maximum height
    val resizedBitmap = ImageUtils.resizeImage(this, uri, maxWidth, maxHeight)

    // Use the resized bitmap as needed
    }

That's it! You have successfully implemented the image picker and image resizing functionality using ImagePicker in Kotlin for Android.