Android integration

Overview

Cloudinary is a cloud-based service that provides an end-to-end image and video management solution. The Android SDK provides simple, yet comprehensive file upload, administration, manipulation, optimization, and delivery capabilities that you can implement using code that integrates seamlessly with your existing Android application.

The Cloudinary Android SDK requires Android 4.1 or higher.

Quick example: Image manipulation

The following Cloudinary URL and corresponding Android code generates the image below including all of the following transformations:

  • Thumbnail crop to a size of 150x150 pixels using face detection gravity to automatically determine the location for the crop
  • Rounded corners with a 20 pixel radius
  • Sepia effect
  • Overlay of the Cloudinary logo on the southeast corner (with a slight offset). The logo is scaled down to a 50 pixel width, with increased brightness, and partial transparency (opacity = 60%)
  • Rotated by 10 degrees
  • Converted to and delivered in PNG format (the originally uploaded image was a JPG)

Android:
MediaManager.get().url().transformation(new Transformation()
  .width(150).height(150).gravity("face").radius(20).effect("sepia").crop("thumb").chain()
  .overlay("cloudinary_icon").gravity("south_east").x(5).y(5).width(50).opacity(60).effect("brightness:200").chain()
  .angle(10)).generate("front_face.png");
Example dynamic delivery URL

Quick example: File upload

The following Android code uses an unsigned upload preset to upload the dog.mp4 video to the specified account sub-folder using the public_id, my_dog. The video will overwrite the existing my_dog video if it exists. When the video upload is complete, the specified notification URL will receive details about the uploaded media asset.

String requestId = MediaManager.get().upload("dog.mp4")
   .unsigned("preset1")
   .option("resource_type", "video")
   .option("folder", "my_folder/my_sub_folder/")
   .option("public_id", "my_dog")
   .option("overwrite", true)
   .option("notification_url", "https://requestb.in/12345abcd")
   .dispatch();

Android SDK features

Cloudinary provides a Android library with an easy-to-use SDK for further simplifying your integration with Cloudinary.

  • Build dynamic URLs for delivering images and videos with on-the-fly transformations
  • Implement direct file upload from your mobile application directly to your Cloudinary account
  • Support chunked upload for large files
  • Preprocess files before uploading
  • Handle asynchronous upload callbacks
  • Upload policy options (upload only on non-metered networks, only when charging, etc) globally or per upload request
  • Automatic error handling for network disconnections, timeouts, etc
  • Save bandwidth with a cache-enabled resource downloader
  • Support code in Objective-C

Android capitalization and data type guidelines

When using the Android SDK, keep these guidelines in mind:

  • Parameter names: snake_case. For example: public_id
  • Classes: PascalCase. For example: MediaManager
  • Methods: camelCase. For example: upload
  • Pass parameter data as: Map

Getting started

This section describes the information you need to know for installing and setting up Cloudinary in your Android application.

Installation

The following instructions detail the installation of the Cloudinary Android library. Note that there is no need for any ProGuard configuration. Use one of the following options:

Gradle installation

Add the following line of code within the dependencies section of your build.gradle file:

compile group: 'com.cloudinary', name: 'cloudinary-android', version: '1.20.0'

Maven installation

Add the following tag within the dependencies tag in your pom.xml file:

<dependency>
    <groupId>com.cloudinary</groupId>
    <artifactId>cloudinary-android</artifactId>
    <version>1.20.0</version>
</dependency>

Manual installation

Download the following 2 files and put them in your libs folder:

Setup

To use the Cloudinary Android library you have to configure at least your cloud_name. You can additionally define a number of optional configuration parameters if relevant. You can find your account-specific configuration credentials in the dashboard of our Management Console.

The entry point of the library is the MediaManager object. The MediaManager.init() method must be called once per application lifecycle before using the Android library, preferably in Application.onCreate(). Setting the configuration parameters can be done either when initializing the library, or by using the CLOUDINARY_URL meta-data property in the AndroidManifest.xml file.

Config with init

To set the configuration parameters programmatically while initializing the MediaManager, pass a HashMap with parameters as the second argument of the init method. For example:

Map config = new HashMap();
config.put("cloud_name", "myCloudName");
MediaManager.init(this, config);

Config with meta-data

Alternatively, you can add the CLOUDINARY_URL meta-data property to the AndroidManifest.xml file, within the application tag. For example:

<meta-data 
   android:name="CLOUDINARY_URL" 
   android:value="cloudinary://@myCloudName"/>

When using the meta-data property, no configuration is required when initializing the MediaManager:

MediaManager.init(this);

Sample project

The Android sample project uses Cloudinary's Android SDK to perform direct uploading with uploading progress indication and an image preview with advanced transformations.