Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Integrating Android Image Cropper Library into Your Application

Tech May 10 4

The Android Image Cropper library, available at GitHub, provides flexible image cropping capabilities.

Setup

Add the dependency to your build.gradle file:

implementation 'com.theartofdev.edmodo:android-image-cropper:2.4.+'

Add the required permissions to your AndroidManifest.xml:

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

Usage Approaches

Activity-based Cropping

  1. Register the cropping activity in AndroidManifest.xml:
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"/>
  1. Launch the cropping activity with your image URI:
CropImage.activity(imageUri)
    .setGuidelines(CropImageView.Guidelines.ON)
    .start(this);
  1. Handle the result in onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri croppedUri = result.getUri();
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
        }
    }
}

View-based Cropping

  1. Add the CropImageView directly to you're layout:
<com.theartofdev.edmodo.cropper.CropImageView
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cropImageView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />
  1. Load an image into the view:
cropImageView.setImageBitmap(bitmap);
// or
cropImageView.setImageUriAsync(uri);
  1. Retrieve the cropped image:
Bitmap cropped = cropImageView.getCroppedImage();
// or
cropImageView.setOnCropImageCompleteListener(listener);
cropImageView.getCroppedImageAsync();

For further details, refer to the official documentation on the GitHub project page.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.