How to crop image in Android Kotlin

In this android kotlin source code example, we are going to crop image in Android Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to crop image in Android Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".imageView.ImageViewActivity7">

    <ImageView
        android:id="@+id/imagePreview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <Button
        android:id="@+id/choose_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Choose image"
        android:background="@color/colorAccent"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.bluapp.kotlinview.R
import android.widget.Toast
import android.content.Intent
import android.widget.Button
import android.widget.ImageView
import com.theartofdev.edmodo.cropper.CropImage


class ImageViewActivity7 : AppCompatActivity() {
    val PIC_CROP_REQUEST = 1
    val PICK_IMAGE_REQUEST = 2
    var imagePreview: ImageView?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_image_view7)
        imagePreview = findViewById<ImageView>(R.id.imagePreview) as ImageView
        val chooseImageBtn = findViewById<Button>(R.id.choose_image) as Button
        chooseImageBtn.setOnClickListener { ImagePicker() }
    }

    private fun ImagePicker() {
        val intent = Intent()
        intent.type = "image/*"
        intent.action = Intent.ACTION_GET_CONTENT
        intent.putExtra("crop", "true")
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
            if(data == null || data.data == null){
                return
            }
            CropImage.activity(data.data!!).start(this);
        }else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            val result = CropImage.getActivityResult(data)
            if (resultCode == RESULT_OK) {
                imagePreview?.setImageURI(result.getUri())
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                val error = result.getError()
                Toast.makeText(this@ImageViewActivity7, error.message, Toast.LENGTH_SHORT).show()
            }
        }

    }
}

build.gradle

implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0'

AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment