Android Image Upload to Server With Kotlin (2)

Oyewale Ademola
Oct 30, 2020

In the past, I wrote an article teaching how to upload an image to a server on Android. Since then, a lot has changed in the Android ecosystem and since 2020 is being 2020, I thought to provide an update with a Kotlin edition.

Let’s jump right in. If you have some experience with Kotlin and Android in general, you can read on and get straight to implementation. Otherwise, it shouldn’t be hard to follow all the same.

Uploading images can be broken down into a set of steps listed below;

1. Select the Image to upload via an Intent

2. Act on the result if successful

3. Upload to server

4. If the upload is successful/not, relay the message accordingly to the user.

— Interface to handle asynchronous task completion —

interface AsyncTaskCompleteListener {
fun onTaskCompleted(response: String, serviceCode: Int)
}

— Let’s define a multi-part helper which handles the AsyncTask-related activities;

Finally, we will have the Activity which wraps and handles all user interactions at the first layer.

Some useful non-native imports;// Apache:  org.apache.http.client
// Dexter: com.karumi.dexter

I hope you find it useful :)

--

--