Upload Image To Server In Android Using Multipart Volley

Upload image To Server In Android Using Multipart Volley Tutorial is here.

We will upload image using multipart volley from android device.

There are two options to upload the image to the server with volley.

  1. Convert Image to Base64 and then send to server.
  2. Upload image directly using multipart

I have used the second option in this tutorial.

For first option visit : Android upload image with volley

Multipart allows us to send the image in it’s original format.

With volley, this complex task becomes much easier with few coding lines.

PHP Web Service

Below is the code block of the PHP web service which will upload the image to the server.

Name of this file is uploadfile.php

Step 1. Manifest changes

We need few permissions in this tutorial to accomplish our goal.

Add the below code lines in AndroidManifest.xml

  • Now we need to ask for runtime permissions for second and third line.
  • We will do this task MainActivity.java class later.

Internet is normal permission so no need for runtime version of it.

Step 2. Writing gradle file

Let us add some dependencies that will help us in this tutorial.

Add the following code in build.gradle(Module: app) file

  • First dependency is for volley library. It will allow us to use volley classes in our project.
  • Second is for picasso. We will load the imahe from URL using picasso library.
  • Third one is for dexter which is the library for runtime permissions.

Step 3. Multipart Class

Create a new JAVA class and give it a name VolleyMultipartRequest.java

Code structure for VolleyMultipartRequest.java is as the below

  • This class will help us to make http calls using volley library.
  • We will create object of this class whenever we want to make volley http calls.
  • Methods of this class will also play important part.

You do not need to make any change in this class. Just keep it as it is.

Step 4. Main Updation

Let us make changes in activity_main.xml and MainActivity.java files.

Make sure the code of activity_main.xml looks same as the below one

  • There are three widgets in the above file.
  • One button, one textview and one imageview.
  • When the user clicks on the button, a screen will be open from which he can select the image.
  • Textview is saying that the image in the imageview is uploaded to the server.
  • Imageview will have the preview of the uploaded image.

Now in the MainActivity.java file, write down the following source code

Deeper Look In Main Activity

See the following lines

  • First is making an object of Button class.
  • Second is giving us the object of the ImageView class.
  • Third is creating a final integer variable GALLERY having the value as 1.
  • This final variable is not static, means that value of GALLERY is always 1, you can not change it.
  • Fourth line is making one string variable upload_URL which holds the URL to the web service.

Remember that at end of the web service, I have added a ? (question mark) sign. This sign is needed to add the parameter while making http call to this URL.

  • Last line is simply making an object of the RequestQueue class.
  • In onCreate() method, compiler will call the requestMultiplePermissions() method.

Following is the code block for requestMultiplePermissions() method.

  • Above method uses the classes from Dexter library.
  • This method will ask for runtime permissions (for READ and WRITE external storage).

Read the following coding lines

  • Compiler will execute the above lines when the user clicks on the button.
  • It will first create one intent. This intent is a Pick intent means that it will create a screen from where the user can pick the image.
  • When the user selects the image and clicks on OK button, compiler will run the onActivityResult() method.

Code for onActivityResult() method is as the following

  • First of all, compiler will check whether user have selected the image or not.
  • If user have not selected image then if(resultCode == this.RESULT.CANCELLED) condition will be true. So compiler will not do anything in this case.
  • When the user have selected one image then if(resultCode == GALLERY) condition will be true and compiler will first get URI of the image from the data.
  • From URI, it will get bitmap of the image.
  • Then it will call uploadImage() method with bitmap as the parameter of this method.

uploadImage()

Below is the coding lines for uploadImage() method

  • First of all, compiler will create the object of the VolleyMultipartRequest class.
  • Using this object, we will make the http call.
  • There are two methods getParams() and getByteData() in this uploadImage() method.
  • getParams() is used to create string parameters. Means that key and value of the parameter in the string format only.
  • getByteData() will help us to send files to the server.
  • Here, key will be in string format but value will be in file format. We will pass our image using getByteData() method.
  • When the http call is successfull, compiler will run onResponse() method.
  • In this method, we will get the JSON response.
  • JSON response contains the URL of the uploaded image.

Using this URL, we will fetch this information with Picasso library and will preview it in Imageview.

See Multipart

Download Source Code for upload image to server in android using multipart volley

Download Source Code For Upload_Image_Volley_Multipart

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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