Crop Image Android Tutorial – Pick and Crop Image in Android

Hello everyone, here I bring you this Crop Image Android Tutorial in which you will learn how to implement image picker and cropper. This utillity can be used for configuring Profile Images or some image related tasks where you need to pick any image from gallery or camera and then get a cropped image out of it. So, without wasting any time, let’s get started.

The output of this Crop Image Android Tutorial may be seen in the below images.

crop image android tutorial

Crop Image Android Tutorial

Creating new Project

  • Begin with creating a new Android Studio Project. As the gradle is built and the files get loaded, we are good to import the library required to achieve the  utility. To do so,

Adding Library

  • Add the line below to your Project level gradle

  • and add the line below to your app level gradle and sync it:-

  • Now that the library has been imported.

Creating Interface

  • Its time to configure the activity_main.xml and MainActivity.java.
  • Here’s the activity_main.xml. It consists of an imageButton where you can tap on to load your profile picture and some textViews to display some profile information.
  • So, go ahead and add the following code to your activity_main.xml.

Coding the Activity

  • Now that the layout is configured, we shall configure the MainActivity.java.
  • Here’s the MainActivity.java. It consists of methods to initiate the image selection, request permissions, handling the cropping activity and all. So, go ahead and add the following code to your MainActivity.java:-

  • Next, we need an image to be displayed in the imageButton by default. To do so, save the image below  and add it to the  drawable folder of your project(filename must remain same).

profile

 

  • Great. We are almost done. Just update your AndroidManifest.xml with the one shown below:-

  • And you did it. Now run the project and tap on the profile image button. A pop up shall come up asking your to choose between Camera and Gallery. As you do and select the image, it shall take you to the CropImagActivity. Once cropped, it will return to the MainActivity with the cropped image set to the imageButton.

So thats all for this crop image android tutorial. If you are having any troubles regarding this crop image android tutorial the lets meet in the comment section to sort out the problems. Thank You 🙂

29 thoughts on “Crop Image Android Tutorial – Pick and Crop Image in Android”

  1. hi manish if i am uploding the file it showing error and it is possible we cant show only file explorer
    if ok plz explain passible send code to mail or url

    Reply
  2. Thanks Manish .. you guys are doing a wonderful job here!!!

    I wanted to upload the cropped image to the server as described in your tutorial below
    https://www.simplifiedcoding.net/android-upload-image-to-server/

    Files that are on the mobile work flawlessly however, the app crashes when I try to upload the camera image.
    Seem the problem is that the camera returns a File URI and “content” which is being expected by the getPath method.

    There seem to be some solutions below.
    http://stackoverflow.com/questions/37624833/android-android-database-cursor-movetofirst-on-a-null-object-reference
    http://stackoverflow.com/questions/6301215/converting-file-scheme-to-content-scheme/16916266#16916266

    but my bad, am unable to make much sense of them.
    Request your help, thanks in advance.

    Reply
      • Manish .. am getting the following error
        java.lang.NullPointerException: Attempt to invoke interface method ‘boolean android.database.Cursor.moveToFirst()’ on a null object reference

        it is pointing to the “cursor.moveToFirst();” line in the getPath method ..

        Reply
          • It seems like the method used to get the path of the image in upload image post is not working in case of camera.
            So,in the post on this link-https://www.simplifiedcoding.net/upload-pdf-file-server-android/ ,there’s a class called FilePath.java use that class to fetch the path of the image as you upload the image. Hope this will do.

  3. @Dpotato

    Add the following two methods to your activity and call requestStoragePermission() in OnCreate.

    //Requesting permission
    private void requestStoragePermission() {
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
    return;

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
    //If the user has denied the permission previously your code will come to this block
    //Here you can explain why you need this permission
    //Explain here why you need this permission
    }
    //And finally ask for the permission
    ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
    }

    //This method will be called when the user will tap on allow or deny
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    //Checking the request code of our request
    if (requestCode == STORAGE_PERMISSION_CODE) {

    //If permission is granted
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    //Displaying a toast
    Toast.makeText(this, “Permission granted now you can read the storage”, Toast.LENGTH_LONG).show();
    } else {
    //Displaying another toast if permission is not granted
    Toast.makeText(this, “Oops you just denied the permission”, Toast.LENGTH_LONG).show();
    }
    }
    }

    Reply
  4. Thanks, Manish, for a wonderful tutorial but my problem here is I want the image to fit into a circle shape like a WhatsApp user profile. Secondly, it’s so small on my device after cropping and I’m trying to Crop the image and set it to the desired size but is not really working.

    Reply
  5. everything worked fine at first but for some reason after some weeks when i try to pick from camera it just return, camera doesn’t open the piker just closes. picking from galary works just fine.

    Reply
  6. CropImage.activity(imageUri)
    .setGuidelines(CropImageView.Guidelines.ON)
    .setMultiTouchEnabled(true)
    .start(this);

    here i have issues as my app crashes. i have added all the dependencies and maven libraries too. it just doesnt right the logs. Plus it never asks for the permissions too. how does it run anyway?

    Reply
  7. Hello everybody,
    To those whoa re facing camera problems in marshmallow devices. The above code doesn’t check for camera runtime permissions. Here is the code

    public void onSelectImageClick(View view) {
    if (CropImage.isExplicitCameraPermissionRequired(this)) {
    requestPermissions(new String[]{Manifest.permission.CAMERA}, CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE);
    } else {
    CropImage.startPickImageActivity(this);
    }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    if (requestCode == CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE) {
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    CropImage.startPickImageActivity(this);
    } else {
    Toast.makeText(this, “Cancelling, required permissions are not granted”, Toast.LENGTH_LONG).show();
    }
    }
    if (requestCode == CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE) {

    if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    // required permissions granted, start crop image activity
    startCropImageActivity(mCropImageUri);
    } else {
    Toast.makeText(this, “Cancelling, required permissions are not granted”, Toast.LENGTH_LONG).show();
    }
    }
    }

    You can refer the sample code here
    https://github.com/ArthurHub/Android-Image-Cropper

    Reply
  8. One more thing that needs to be changed is the following code required for requesting storage permissions

    In OnActivityResult code

    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);

    Reply
  9. thank you soo very much for valueable code..
    i m a big fan of this site…
    great job sir..
    keep helping Android freshers like me with your simple and greate codes…
    thank you soo much..

    Reply
  10. hi manish.
    your code is working fine thank u so much.
    can i fetch cropped image coordinates not image button coordinates.
    when i do then only i fetch image button coordinate which is always same not result image coordinates.
    plzz suggest me .thank u so much

    Reply
  11. Hi Manish,

    Nice tutorial. By the way i would like to store the image in sharedpreferences so as when the application is closed,
    the image again loads with the last set image.

    Kindly help me with this.

    Reply
  12. I can not save multiple image in cache , By default this library save the cropped image in cache , but i can not save more than one.

    Reply
  13. Hi! i am new to android ,i have excuted the above code but when i reache the croping area i don’t see the crop button to enable croping so as to return to the main activity.plz help

    Reply
  14. hi

    your code is working up to select an image and but status bar not showing and I don’t see any crop button.
    what is the issue? do you know? do you want to check my code? please help me.

    Reply

Leave a Comment