Uploading Multiple Images To Server Using Volley Library In Android

By | December 12, 2018

In this tutorial we have shared codes on how to upload multiple images to server as json object to server using Volley a popular android library for server communication using http requests.

There are several means which this tasks can be achieved but has limitation especially the common multipart option for uploading images.In this sample we are going to convert images to convert images to base64 byte array and do the uploading as JSONObject,this enables images to be uploaded with any other additional information as per user requirements.

The Api end-point is supposed to decode the base64 encoded array back to the original images and save in a directory.Enough with the introduction time to get started as always ensure you have the following libraries in gradle file.

implementation 'com.github.bumptech.glide:glide:4.8.0'

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

implementation 'com.android.volley:volley:1.1.1'

On your manifest specify the following permissions as shown in the snippet below.

 

Inside your application tag in manifest specify the following file provider on xml as meta-data with your package name as below;

 

 

Now we need to create layout file with image view preview where users will be able to click on the image view to launch camera intent and take photo and display on that particular imageview.
The following is our activity.xml file.

 

On main Activity initialize the imageviews which will be used to hold the images temporarily for preview.Below is code responsibleto check runtime permission and opening camera intent.

 

Now we have the code to check elevated permission and offer run-time permission checks on post Marshmallow android devices we can now go ahead and create the openCameraIntent method as below:

 

Briefly on the above code snippet the method “openCameraIntent” is responsible for creating imagefile Using packageManager from the default Camera intent.It invokes the onActivityResult using android startActivityForResult method.The following code shows the onActivityResult which is an override method to handle camera capture results.

 

On the above code we take the bitmap from Uri and compress it using in JPEG format,note that you can adjust image quality by increasing the second paramter of the bitmap.compress() method.The base 64 images bytes format is added to the “encodedImageList” Arraylist. The code below shows how to use the “encodedImageList” to upload the images using volley as JSONObject.

 

Make sure your AppController Class is include in the manifest file i.e Find the “AppController” in my previous tutorials.
Below is full code of the main Activity class Hope you enjoyed this tutorial,thanks.

 

Leave a Reply

Your email address will not be published. Required fields are marked *