The Cloudinary Android SDK allows you to quickly and easily integrate your application with Cloudinary. Effortlessly optimize and transform your cloud's assets.
This Readme provides basic installation and usage information. For the complete documentation, see the Android SDK Guide.
- Image Transformation
- Video Transformation
- Direct File Upload
- Preprocess
- Callbacks
- Upload Policy
- Error Handling
Cloudinary SDK | Android SDK |
---|---|
3.x | > 21 |
2.x | > 19 |
1.x | > 14 |
Add the following dependency to your build.gradle:
implementation 'com.cloudinary:cloudinary-android:3.0.2'
The cloudinary_android library is available in Maven Central. To use it, add the following dependency to your pom.xml:
<dependency>
<groupId>com.cloudinary</groupId>
<artifactId>cloudinary-android</artifactId>
<version>3.0.2</version>
</dependency>
Download the latest cloudinary-android from here and the latest cloudinary-core from here and put them in your libs folder.
Each request for building a URL of a remote cloud resource must have the cloud_name
parameter set.
You can set the cloud_name
parameter either when initializing the library, or by using the CLOUDINARY_URL meta-data property in AndroidManifest.xml
.
The entry point of the library is the MediaManager
object. MediaManager.init()
must be called before using the library, preferably in Application.onCreate()
.
Here's an example of setting the configuration parameters programmatically in your Applicaion.onCreate(
:
Map config = new HashMap();
config.put("cloud_name", "myCloudName");
MediaManager.init(this, config);
Alternatively, you can use the meta-data property. In that case, no configuration is required:
MediaManager.init(this);
Only the cloud_name should be included. Your API key and secret aren't necessary. Note: Your API secret should never be exposed in the application.
<manifest>
...
<application>
...
<meta-data android:name="CLOUDINARY_URL" android:value="cloudinary://@myCloudName"/>
</application>
<manifest>
Any image uploaded to Cloudinary can be transformed and embedded using powerful view helper methods:
The following example generates the url for accessing an uploaded sample
image while transforming it to fill a 100x150 rectangle:
MediaManager.get().url().transformation(new Transformation().width(100).height(150).crop("fill")).generate("sample.jpg")
Another example, embedding a smaller version of an uploaded image while generating a 90x90 face detection based thumbnail:
MediaManager.get().url().transformation(new Transformation().width(90).height(90).crop("thumb").gravity("face")).generate("woman.jpg")
If your application is written in Kotlin you can use the syntax below:
MediaManager.get().url().transformation(Transformation<Transformation<*>>().width(90).height(90).crop("thumb").gravity("face")).generate("woman.jpg")
You can provide either a Facebook name or a numeric ID of a Facebook profile or a fan page.
Embedding a Facebook profile to match your graphic design is very simple:
MediaManager.get().url().type("facebook").transformation(new Transformation().width(130).height(130).crop("fill").gravity("north_west")).generate("billclinton.jpg")
The entry point for upload operations is the MediaManager.get().upload()
call. All upload operations are dispatched to a background queue, with
a set of fully customizable rules and limits letting you choose when each upload request should actually run. Requests are automatically rescheduled to be
retried later if a recoverable error is encountered (e.g. network disconnections, timeouts).
The upload results are dispatched asynchronously using UploadCallback
. Global callbacks can be defined, as well as specific callbacks per request.
Note: In order to receive global callbacks even when the app is already shut down, or in the background, the ListenerService
class can be extended and registered in the manifest (see the class for further instructions).
The following examples uploads a File
using the default settings, a request upload callback, and an upload preset (more about upload presets below):
String requestId = MediaManager.get().upload(imageFile).unsigned("sample_preset").callback(callback).dispatch();
The returned requestId
is used to identify the request in global callbacks and to cancel the request if needed. The callback should be any implementation of UploadCallback
.
The uploaded image is assigned a randomly generated public Id. As soon as onSuccess
is called, the image is immediately available for download through a CDN:
MediaManager.get().url().generate("abcfrmo8zul1mafopawefg.jpg")
http://res.cloudinary.com/demo/image/upload/abcfrmo8zul1mafopawefg.jpg
You can also specify your own public ID:
String requestId = MediaManager.get().upload(uri).unsigned("sample_preset").option("public_id", "sample_remote").dispatch();
Using RequestUploadPolicy
, an upload request can be configured to run under specific circumstance, or within a chosen time window:
The following examples uploads local Uri resource, configured to run immediately (the default), with a maximum of 7 retries, and only on an unmetered network (e.g. wifi):
String requestId = MediaManager.get().upload(uri)
.unsigned("sample_app_preset")
.constrain(TimeWindow.immediate())
.policy(new RequestUploadPolicy.Builder().maxRetries(7).networkPolicy(RequestUploadPolicy.NetworkType.UNMETERED).build())
.dispatch();
For security reasons, mobile applications cannot contain the full account credentials, and so they cannot freely upload resources to the cloud. Cloudinary provides two different mechanisms to enable end-users to upload resources without providing full credentials.
1. Unsigned uploads using Upload Presets.
You can create an upload preset in your Cloudinary account console, defining rules that limit the formats, transformations, dimensions and more. Once the preset is defined, it's name is supplied when calling upload. An upload call will only succeed if the preset name is used and the resource is within the preset's pre-defined limits.
The following example uploads a local resource, available as a Uri, assuming a preset named 'sample_preset' already exists in the account:
String requestId = MediaManager.get().upload(uri).unsigned("sample_preset").dispatch();
Another way to allow uploading without credentials is using signed uploads.
It is recommended to generate the upload authentication signature on the server side, where it's safe to store the api_secret
.
Cloudinary's Android SDK allows providing server-generated signature and any additional parameters that were generated on the server side (instead of signing using api_secret
locally).
Your server can use any Cloudinary libraries (Ruby on Rails, PHP, Python & Django, Java, Perl, .Net, etc.) for generating the signature. The following JSON in an example of a response of an upload authorization request to your server:
{
"signature": "sgjfdoigfjdgfdogidf9g87df98gfdb8f7d6gfdg7gfd8",
"public_id": "abdbasdasda76asd7sa789",
"timestamp": 1346925631,
"api_key": "123456789012345"
}
When initializing MediaManager
, a SignatureProvider
can be sent. Whenever an upload requires signing, the library will call the provider's provideSignature()
method,
where you should implement the call to your server's signing endpoint. This callback runs on a background a thread so there's no need to handle threading:
MediaManager.init(this, new SignatureProvider() {
@Override
public Signature provideSignature(Map options) {
// call server signature endpoint
}
}, null);
If you run into an issue or have a question, you can either:
- Open a Github issue (for issues related to the SDK)
- Open a support ticket (for issues related to your account)
Cloudinary is a powerful media API for websites and mobile apps alike, Cloudinary enables developers to efficiently manage, transform, optimize, and deliver images and videos through multiple CDNs. Ultimately, viewers enjoy responsive and personalized visual-media experiences—irrespective of the viewing device.
- Cloudinary Transformation and REST API References: Comprehensive references, including syntax and examples for all SDKs.
- MediaJams.dev: Bite-size use-case tutorials written by and for Cloudinary Developers
- DevJams: Cloudinary developer podcasts on YouTube.
- Cloudinary Academy: Free self-paced courses, instructor-led virtual courses, and on-site courses.
- Code Explorers and Feature Demos: A one-stop shop for all code explorers, Postman collections, and feature demos found in the docs.
- Cloudinary Roadmap: Your chance to follow, vote, or suggest what Cloudinary should develop next.
- Cloudinary Facebook Community: Learn from and offer help to other Cloudinary developers.
- Cloudinary Account Registration: Free Cloudinary account registration.
- Cloudinary Website: Learn about Cloudinary's products, partners, customers, pricing, and more.
Released under the MIT license.