Circular Crop an Image and Save it to the File in Android
There are multiple applications available in the market that help in dealing with image processing, while most of them fail to produce very basic operations. Cropping is a simple application when one could resize an image by cutting it down. This task becomes complex when it comes to free-hand or shape cropping, meaning cropping the image in the desired shape.
In this article, we will show you how you create an application to crop an image in a circular manner and store it in the local device. No external library or service is used to generate this application.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Add an ImageView and two Buttons in the activity_main.xml or the layout file
Step 3: Add the desired image in the res > drawable folder
While adding, give it the desired name. For our reference, this image is “image.png” which is downloaded from the Internet and copied directly into the drawable folder.
Step 4: Write the following code for MainActivity.kt
There are two functions inside this code:
- getCircularBitmap(Bitmap) : To crop the image
- saveMediaToStorage(Bitmap) : To save the image. Refer How to Capture Screenshot of a View and Save it to Gallery in Android?
Refer to the comments for better understanding.
Step 4: Add Storage permission in the AndroidManifest.xml file
This permission is needed to store the image in the device.