Image cropping is a common feature in many Android applications, allowing users to select a specific portion of an image. In this article, we’ll cover different approaches to implementing image cropping in an Android application using the Cloudinary Android SDK.
In order to follow along with this tutorial, you’ll need to have a basic understanding of Android development. You’ll also need to be familiar with Java and Android Studio, though many of these concepts can be applied to Kotlin.
Maximize your digital asset’s impact with Cloudinary’s dynamic resizing and cropping. Sign up for free today!
In this article:
Cropping an Image using Cloudinary Android SDK
The Cloudinary Android SDK provides a simple, comprehensive solution for uploading, transforming, optimizing, and delivering media assets, such as images and videos, using code that integrates seamlessly with your existing Android application.
To get started with the Android SDK in your code, you’ll need to get your Cloudinary credentials (cloud name, API Key, API Secret), which will be used to configure your application. You can get yours by logging in to your Cloudinary dashboard.
Step 1 – Import the Required Packages and Libraries
Naturally, the first thing we’ll need to do is install the resources that power our application. Let’s take a look at our imports:
package com.example.cloudinaryexample; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import com.bumptech.glide.Glide; import com.cloudinary.Transformation; import com.cloudinary.android.MediaManager; import com.cloudinary.android.callback.ErrorInfo; import com.cloudinary.android.callback.UploadCallback; import java.util.HashMap; import java.util.Map;
Step 2 – Add Project Dependencies in Your build.gradle
File
The following code adds the latest version of the Cloudinary Android SDK, Glide (a popular image loading and caching library for Android), and Glide’s annotation processor as dependencies for the project.
dependencies { implementation 'com.cloudinary:cloudinary-android:2.5.0' implementation 'com.github.bumptech.glide:glide:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0' }
Step 3 – Configure Cloudinary
Next, initialize your Cloudinary configuration in the Application
class or MainActivity
:
public class MainActivity extends AppCompatActivity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); // Initialize Cloudinary Map<String, Object> config = new HashMap<>(); config.put("cloud_name", "your_cloud_name"); // Replace with your cloud name config.put("secure", true); MediaManager.init(this, config); applyCroppingTransformation("sample.jpg"); // Replace with the path to your image } }
Step 4 – Cropping the Image
Next, create an applyCroppingTransformation
method to crop the image and generate a transformed image URL:
private void applyCroppingTransformation(String imageName) { Transformation transformation = new Transformation(); transformation.gravity("face").height(150).width(150).crop("thumb"); String transformedUrl = MediaManager.get().url().transformation(transformation).generate(imageName); // Upload the image to Cloudinary with the applied transformation MediaManager.get().upload(transformedUrl) .unsigned("preset1") // Use your unsigned preset .option("resource_type", "image") .callback(new UploadCallback() { @Override public void onSuccess(String requestId, Map resultData) { Log.d(resultData.toString()); loadCroppedImage(transformedUrl); } @Override public void onError(String requestId, ErrorInfo error) { Log.e(error.getDescription()); } }) .dispatch(); }
This example shows how to crop the input image using the gravity
method to focus on the faces of the people in it. You can learn more about other options in Cloudinary’s Android SDK documentation.
Step 5 – Load the Transformed Image URL
Create a method to load the transformed image URL into ImageView
using Glide:
private void loadCroppedImage(String url) { Glide.with(this) .load(url) .into(imageView); }
Then, in activity_main.xml
, add the ImageView like so:
<ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" />
Here’s the complete code for your reference:
package com.example.cloudinaryexample; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import com.bumptech.glide.Glide; import com.cloudinary.Transformation; import com.cloudinary.android.MediaManager; import com.cloudinary.android.callback.ErrorInfo; import com.cloudinary.android.callback.UploadCallback; import java.util.HashMap; import java.util.Map; public class MainActivity extends AppCompatActivity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); // Initialize Cloudinary Map<String, Object> config = new HashMap<>(); config.put("cloud_name", "your_cloud_name"); // Replace with your cloud name config.put("secure", true); MediaManager.init(this, config); applyCroppingTransformation("sample.jpg"); // Replace with the path to your image } private void applyCroppingTransformation(String imageName) { Transformation transformation = new Transformation(); transformation.gravity("face").height(150).width(150).crop("thumb"); String transformedUrl = MediaManager.get().url().transformation(transformation).generate(imageName); // Upload the image to Cloudinary with the applied transformation MediaManager.get().upload(transformedUrl) .unsigned("preset1") // Use your unsigned preset .option("resource_type", "image") .callback(new UploadCallback() { @Override public void onSuccess(String requestId, Map resultData) { Log.d(resultData.toString()); loadCroppedImage(transformedUrl); } @Override public void onError(String requestId, ErrorInfo error) { Log.e(error.getDescription()); } }) .dispatch(); } private void loadCroppedImage(String url) { Glide.with(this) .load(url) .into(imageView); } }
Conclusion
In this article, we have demonstrated how to apply a basic cropping transformation using the Cloudinary Android SDK. You can modify the transformation parameters to achieve different cropping effects, such as changing the dimensions, crop mode, or gravity.
For more information, see:
- Cloudinary Academy Introduction to Cloudinary Android SDK course
- Cloudinary Android SDK documentation
- Cloudinary documentation on cropping images