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

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

1

maven{url"https://jitpack.io"}

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

1

compile'com.theartofdev.edmodo:android-image-cropper:2.3.+'

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.

activity_main.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="@android:color/black">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Tap on the image below to change your profile picture"

android:textColor="@android:color/white"

android:textSize="20dp"

android:id="@+id/textView"/>

<FrameLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/quick_start_cropped_image"

android:background="@android:color/white"

>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

<TextView

android:layout_width="wrap_content"

android:textColor="@android:color/black"

android:layout_height="wrap_content"

android:text="Name: Manish Kumar"

android:textSize="30dp"

android:layout_gravity="center"

/>

<TextView

android:textColor="@android:color/black"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Email: manishh776@gmail.com"

android:textSize="25dp"

android:layout_gravity="center"

/>

<TextView

android:textColor="@android:color/black"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="MobileNo:1234567890"

android:textSize="25dp"

android:layout_gravity="center"

/>

</LinearLayout>

</FrameLayout>

<ImageButton

android:id="@+id/quick_start_cropped_image"

android:layout_width="200dp"

android:layout_height="200dp"

android:src="@drawable/profile"

android:layout_marginTop="39dp"

android:layout_below="@+id/textView"

android:layout_centerHorizontal="true"/>

</RelativeLayout>

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:-

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 🙂

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.

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.

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) {

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.

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

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy

About Simplified Coding

Simplified Coding is a blog for all the students learning programming. We are providing various tutorials related to programming and application development. You can get various nice and simplified tutorials related to programming, app development, graphics designing and animation. We are trying to make these things simplified and entertaining. We are writing text tutorial and creating video and visual tutorials as well. You can check about the admin of the blog here and check out our sitemap