How to capture image with crop feature in android

This article explains how to capture image from camera and gallery with crop functionality in android apps using kotlin programming language.

There are many third party libraries supporting this but most of them are not working in all android devices or all android versions.

Today I am also implementing one library through simple example but it is working in most of all android devices and versions. If you use this library, you do not need to check permissions or use fileprovider at your end. This functions are integrated in library. You just need to call. 





Add below dependency in your project level build.gradle file 

maven { url “https://jitpack.io” } 


Add below dependencies in your app level build.gradle file 

implementation ‘com.github.mike14u:croperino:1.1.8’ 
implementation ‘com.mikhaellopez:circularimageview:3.2.0’


Give Permissions in your manifest file

<uses-permission android:name=”android.permission.CAMERA” />
<uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE” />
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />
<uses-feature android:name=”android.hardware.camera” />
<uses-feature android:name=”android.hardware.camera.autofocus” />



Initialize library

We need to give image name and image storage directory path at the time of initialization of library.

CroperinoConfig(
            “IMG_” + System.currentTimeMillis() + “.jpg”,
            “/ImagePickerSample/Pictures”,
            “/sdcard/ImagePickerSample/Pictures”
        )
CroperinoFileUtil.setupDirectory(this)


Show Image Picker Options for Camera and Gallery

if (CroperinoFileUtil.verifyStoragePermissions(this)) {
                Croperino.prepareChooser(
                    this,
                    “Capture image…”,
                    ContextCompat.getColor(this, android.R.color.background_dark)
                )
            }


override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if (requestCode == CroperinoFileUtil.REQUEST_CAMERA) {
            for (i in permissions.indices) {
                val permission = permissions[i]
                val grantResult = grantResults[i]
                if (permission == Manifest.permission.CAMERA) {
                    if (grantResult == PackageManager.PERMISSION_GRANTED) {
                        Croperino.prepareCamera(this)
                    }
                }
            }
        } else if (requestCode == CroperinoFileUtil.REQUEST_EXTERNAL_STORAGE) {
            var wasReadGranted = false
            var wasWriteGranted = false
            for (i in permissions.indices) {
                val permission = permissions[i]
                val grantResult = grantResults[i]
                if (permission == Manifest.permission.READ_EXTERNAL_STORAGE) {
                    if (grantResult == PackageManager.PERMISSION_GRANTED) {
                        wasReadGranted = true
                    }
                }
                if (permission == Manifest.permission.WRITE_EXTERNAL_STORAGE) {
                    if (grantResult == PackageManager.PERMISSION_GRANTED) {
                        wasWriteGranted = true
                    }
                }
            }
            if (wasReadGranted && wasWriteGranted) {
                Croperino.prepareChooser(
                    this,
                    “Capture image…”,
                    ContextCompat.getColor(this, android.R.color.background_dark)
                )
            }
        }

    }


Crop and Display Image After Capturing from Camera or Gallery

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            CroperinoConfig.REQUEST_TAKE_PHOTO -> {
                if (resultCode == Activity.RESULT_OK) {
                    Croperino.runCropImage(
                        CroperinoFileUtil.getTempFile(),
                        this,
                        true,
                        1,
                        1,
                        R.color.gray,
                        R.color.gray_variant
                    )
                }
            }
            CroperinoConfig.REQUEST_PICK_FILE -> {
                if (resultCode == Activity.RESULT_OK) {
                    CroperinoFileUtil.newGalleryFile(data, this)
                    Croperino.runCropImage(
                        CroperinoFileUtil.getTempFile(),
                        this,
                        true,
                        1,
                        1,
                        R.color.gray,
                        R.color.gray_variant
                    )
                }
            }
            CroperinoConfig.REQUEST_CROP_PHOTO -> {
                if (resultCode == Activity.RESULT_OK) {
                    val i = Uri.fromFile(CroperinoFileUtil.getTempFile())
                    img_profile.setImageURI(i)
                }
            }
        }
    }


capture image
capture image
capture image
capture image

Leave a Reply