Android Upload image Using Volley To Server (PHP-MySQL)

Welcome to Android Upload image Using Volley To Server (PHP-MySQL) Tutorial.

In this tutorial, we will upload the image to server using volley in android.

We will use PHP as a server script and MySQL as the server database.

Using volley, we can upload the image into the Base64String format to the remote server.

If you don’t want to convert image into base64 and want to upload image as a multipart file, then visit :

First, we will convert the image into the Base64String format and then we will upload it using PHP web service.

This process simplifies the process of sending the image to the remote server.

Volley uses very few lines of code to complete this task, which reduces the complexity.

View Volley Upload Image

PHP Script

Make a new PHP file and it’s name is config.php

Following is it’s code block

Following is the code for the PHP script named uploadVolley.php file

Step 1. Add Required Permissions

We need to have some permissions in this tutorial.

Add the below source code in AndroidManifest.xml file.

Three permissions are there: internet, read internal storage and write internal storage.

Among them, internet is normal permission but we need to ask runtime permissions for read and write external storage.

We will write the code for runtime permissions in file later.

Step 2. Needed dependencies

We require following dependencies

Add above three lines in build.gradle(Module: app) file of your project.

First line will enable us to use the volley library.

Second will add the classes of picasso library to load the image from URL.

Third line will help to ask for runtime permissions with dexter library.

Step 3. activity_main.xml

Now let us create the activity_main.xml file.

First of all, write down the below coding lines in activity_main.xml file.

I have taken one button, one textview and one Imageview in the above file.

When the user clicks the button, he will be asked to select the image.

Textview is telling the user that below compiler upload the image below image.

ImageView holds that uploaded image.

Step 4. Writing Main Activity

In your file, write down the following coding lines

Running through above Code

Consider the below coding lines

First line is making a final integer variable named GALLERY.

Value of GALLERY is constant that means it’s value can not be changed.

Second line is defining a String variable which holds the URL from which we will call the web service.

This web service will get the Image as a parameter and will add the image to the server.

Third line is making an object of the JSONObject class.

Final line is making an object of RequestQueue class.

In onCreate() method, compiler is calling requestMultiplePermissions() method.

Code structure for requestMultiplePermissions() method is as the below

This method is helping us to ask for runtime permissions to the user.

Compiler will use the classes and methods of the dexter library in this method.

Dexter library simplifies the whole scenario of runtime permissions.

We will ask for Read and Write external storage permissions in this method.

Now read the following coding lines

Compiler will execute the above code when the user clicks on the button.

Here, compiler will first create one Intent.

This intent will lead the user to new screen.

In this screen, system will showcase all the images present in the gallery.

Now user have to select one image from this screen. When he clicks on OK button after choosing the image, compiler will run the onActivityResult() method.

Following is the code block for onActivityResult() method.

Here, compiler will check if user have selected one image or not.

if the value of requestCode is equals to the GALLERY (1) variable then compiler will enter into if(requestCode == GALLERY) condition.

Now, system will first collect the URI from the data passes in this onActivityResult() method.

Then compiler will create the Bitmap from this URI.

After that, compiler will set that bitmap into the imageview.

And then it will call the uploadImage() method.

Coding lines for uploadImage() method is as the following

First three lines will convert the image bitmap into Base64 string format.

Then, compiler will initialize the jsonObject.

After that, compiler will create the current time in milliseconds and sets it in string variable.

This string variable will be the unique name of the image.

Following two lines are the parameters of the web service

First is the name of the image and the second is the image itself in BASE64 String.

Then compiler will create the object of JsonObjectRequest class and will call the web service.

onResponse() method will be called when the web service gives any response in the JSON format.

In the onResponse() method, compiler will clear the Volley cache.

Download the source Code for Android Upload Image Using Volley

Download Source Code Upload_Image_Volley

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...

2 Responses

  1. siamma says:

    what config.php ?
    dont show Image “Uploaded Successfully” and image not upload

Leave a Reply

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