Tuesday 6 March 2018

Android image cropping from Camera / Gallery

Recently I needed to implement avatar image upload from an Android app, I didn't found any example that did all that I needed so i make this tutorial .

-> Requirements:

  • Using single chooser pickup image from camera or gallery .
  • Select user picture and you can use cropping functionality for your avatar.
  • Avatar image size fixed 500×500 pixels.
  • Usage of memory is efficient.

-> Obstacles:

  • Creating single chooser intent for camera and gallery is not so trivial.
  • Android crop activity is limited, unreliable and is internal API.
  • May be image will be rotated after selecting from the camera or gallery
  • If picked image is large so sometimes it creates issues of memory leak.
-> Powerful (Zoom, Rotation, Multi-Source), customizable (Shape, Limits, Style), optimized (Async, Sampling, Matrix) and simple image cropping library for Android.
  1. Include the library
compile 'com.theartofdev.edmodo:android-image-cropper:2.6.+' 
-> Add permissions to manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Using Activity

  1. Add CropImageActivity into your AndroidManifest.xml
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
  android:theme="@style/Base.Theme.AppCompat"/> 
<!-- optional (needed if default theme has no action bar) -->
  1. Start CropImageActivity using builder pattern from your activity
// start picker to get image for cropping and then use the image in cropping activity
CropImage.activity()
  .setGuidelines(CropImageView.Guidelines.ON)
  .start(this);

// start cropping activity for pre-acquired image saved on the device
CropImage.activity(imageUri)
 .start(this);

// for fragment (DO NOT use `getActivity()`)
CropImage.activity()
  .start(getContext(), this);
  1. Override onActivityResult method in your activity to get crop result
@Override
public 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 resultUri = result.getUri();
    } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
      Exception error = result.getError();
    }
  }
}

Using View

  1. Add CropImageView into your activity
<!-- Image Cropper fill the remaining available height -->
<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. Set image to crop
cropImageView.setImageUriAsync(uri);
// or (prefer using uri for performance and better user experience)
cropImageView.setImageBitmap(bitmap);
  1. Get cropped image
// subscribe to async event using cropImageView.setOnCropImageCompleteListener(listener)
cropImageView.getCroppedImageAsync();
// or
Bitmap cropped = cropImageView.getCroppedImage();
-> Referecne :  https://github.com/ArthurHub/Android-Image-Cropper
Thank you!!





































No comments:

Post a Comment