Android SDK

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.2 or higher.

Quick example: Transformations

The following Cloudinary URL and corresponding Android code delivers the image below in an https delivery URL, 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(new Layer().publicId("cloudinary_icon")).gravity("south_east").x(5).y(5).width(50).opacity(60).effect("brightness:200").chain()
  .angle(10)).secure(true).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://mysite.example.com/notify_endpoint")
   .dispatch();

Android SDK features

  • 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

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.24.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.24.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, an image preview with advanced transformations, and video playback of adaptive streaming videos based on the ExoPlayer library.

You can also check out the Cloudinary Demo - eCommerce App on the Google play store, with the Android source code available on GitHub. The demo showcases some of Cloudinary's features, shows how to take advantage of existing open source libraries (e.g., Picasso, Glide, Fresco, Imageloader) and integrates them within a functional app. For more info, see Introducing the Cloudinary Demo Android App, Part 1.

eCommerce Demo App

Related topics