Upload an Image or File to Your Server Using Volley in Kotlin

A step-by-step tutorial

Yong Cui
Yong Cui
Dec 14, 2019 · 3 min read
Image for post
Image for post
Photo by Angela Compagnone on Unsplash

As a cross-platform, statically typed, general-purpose programming language, Kotlin has become more and more popular among Android developers. It has full interoperability with Java code and supports type inference to make its syntax more concise without sacrificing readability. Importantly, Kotlin is developed and maintained by the Kotlin Foundation, which is sponsored by Google — a tech company that has earned credibility from most, if not all, programmers.

If you pick Kotlin for your Android app development, a common task you may find yourself dealing with is having to upload an image, such as a user profile thumbnail, to your server.

I’m going to show you an easy way to get this job done using Volley, an HTTP library that can implement networking requests quickly for Android apps.

Step 1: Setup

To use the Volley library in your app, you need to add the Volley dependency to your app’s build.gradle file:

dependencies {
...
implementation 'com.android.volley:volley:1.1.1'
}

Certainly, your app needs to have permission to use the internet by adding the permissions in the AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET"/>

For the purpose of this tutorial, I simply created a button called imageButton to launch the gallery, an image view called imageView to preview the photo, and another button called sendButton to upload the image to the server. The layout file is shown:

Step 2: Choose and Process the Image

The use of an Intent with a type of image/* will allow you to open the picture gallery. With this intent, start the activity using the function of startActivityForResult, which will allow access to the image selected in the gallery.

The MainActivity.kt file

Once the image is selected, you can access it by implementing the function of onActivityResult. Specifically, we have to access the Uri of the image, with which, we show the image in the imageView and create a ByteArray for later data uploading.

Step 3. Create a Volley Request Subclass That Supports Multipart Uploading

To upload the image data to the server, we need to use the multipart form request. To do that, we first create a subclass that inherits the Volley’s Request class. Here is the code for that:

Let me highlight some key functions. First, you need to specify the body content type to be multipart/form-data and the boundary used, the latter of which is necessary for the server to correctly parse your data. Second, the params and data will all need to be written to a ByteArray, which can be sent as the body for the POST request.

Step 4. Upload Your Image

Now you have the image data and the multipart Request class, you’re ready to upload the image. Certainly, your server needs to have the correct API to respond this HTTP POST request. For the purpose of this tutorial, I will just use a free HTTP POST test service that I found online (https://ptsv2.com/). Although it’s free, please use it parsimoniously and just for testing purposes.

As part of the above MainActivity.kt file, one important thing to note is that you need to override the getByteData function of the request class. Specifically, you need to provide the request with your image data, and the class will have additional functions to convert the image data to the needed ByteArray format for data uploading.

In the “toilet” from the test website (https://ptsv2.com/), you should see your data dump. You just uploaded your image using Volley!

Conclusions

The Volley library is a robust library that makes the HTTP requests much easier to implement. You can subclass the Request class to meet your other needs, including some standard ones (e.g., json requests, string requests). The example given here can be easily adapted to upload files other than images.

Happy coding with Kotlin!

Better Programming

Advice for programmers.

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Yong Cui

Written by

Yong Cui

Work at the nexus of biomedicine, data science & mobile dev. Love to write on these technological topics. Follow me @ycui01 on Twitter to get latest articles.

Better Programming

Advice for programmers.

Yong Cui

Written by

Yong Cui

Work at the nexus of biomedicine, data science & mobile dev. Love to write on these technological topics. Follow me @ycui01 on Twitter to get latest articles.

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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