The Startup
Published in

The Startup

Upload Image to Cloudinary in Android Using Kotlin Programming

cloudinary.com

Cloudinary is a Streamline media management and improves user experience by automatically delivering images and videos, enhanced and optimized for every user.

Cloudinary allows users to upload images/video with two options which are “Signed upload” and “Unsigned upload” to understand more about it check here

In this tutorial, you will learn how to upload an image from your android device to Cloudinary cloud storage easily in less than 5mins.

Get Started

I will share simple steps in setting up a Cloudinary account for uploading images.

Firstly, create an account with Cloudinary here, after that go to the Dashboard and keep the following information private, you will need it later.

* Api key
* Api secret
* cloud name

Step 1: add Cloudinary dependency

Add the module(s) within the dependencies section of your build.gradle file. For example:

implementation group: 'com.cloudinary', name: 'cloudinary-android', version: '1.30.0'

Step 2: Initialize MediaManager

class MainActivity : AppCompatActivity() {
var config: HashMap<String, String> = HashMap()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

config.put("cloud_name", "your cloud name")
config.put("api_key", "your API Key")
config.put("api_secret", "your API secret")
MediaManager.init(this, config)

for the next step, you will need an image picker library or you use the traditional Android image picker, but in this tutorial you will use

Step 3: Use an ImagePicker

After adding the necessary dependencies in your build.gradle file, go ahead and add the following.

ImagePicker.with(this)
.crop() //Crop image(Optional), Check Customization for more option
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080) //Final image resolution will be less than 1080 x 1080(Optional)
.start()
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
//Image Uri will not be null for RESULT_OK
val fileUri = data?.data

imagev.setImageURI(fileUri)

//You can get File object from intent
val file: File = ImagePicker.getFile(data)!!

//You can also get File Path from intent
val filePath: String = ImagePicker.getFilePath(data)!!
imgpath = filePath
uploadToCloudinary(filePath)
} else if (resultCode == ImagePicker.RESULT_ERROR) {
Toast.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show()
}
}
fun uploadToCloudinary(filepath: String) {
MediaManager.get().upload(filepath).unsigned("kks8dyht").callback(object : UploadCallback {
override fun onSuccess(requestId: String?, resultData: MutableMap<Any?, Any?>?) {
Toast.makeText(applicationContext, "Task successful", Toast.LENGTH_SHORT).show()
}

override fun onProgress(requestId: String?, bytes: Long, totalBytes: Long) {

}

override fun onReschedule(requestId: String?, error: ErrorInfo?) {

}

override fun onError(requestId: String?, error: ErrorInfo?) {

Toast.makeText(applicationContext, "Task Not successful"+ error, Toast.LENGTH_SHORT).show()
}

override fun onStart(requestId: String?) {

Toast.makeText(applicationContext, "Start", Toast.LENGTH_SHORT).show()
}
}).dispatch()
}

Note: if you use unsigned upload, make sure to Upload a preset in your Cloudinary account.

On your Cloudinary dashboard -> click on settings -> click on Upload

Congratulation you have successfully uploaded an Image to Cloudinary.

Full Code check here

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store