Android Upload Image Using Volley To Server
Android Upload Image Using Volley To Server
To Server (PHP-MySQL)
BY EDITORIAL TEAM ·
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 : https://demonuts.com/upload-image-to-server-in-
android-using-multipart-volley/
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.
We will write the code for runtime permissions in MainActivity.java file later.
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.
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:tools="http://schemas.android.com/tools"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical">
7
8 <Button
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:id="@+id/btn"
12 android:layout_gravity="center_horizontal"
13 android:layout_marginTop="20dp"
14 android:textAppearance="?android:attr/textAppearanceLarge"
15 android:text="Select Image and upload to server" />
16
17 <TextView
18 android:layout_width="match_parent"
19 android:layout_height="wrap_content"
20 android:text="Below image is uploaded to server"
21 android:layout_marginTop="5dp"
22 android:textSize="23sp"
23 android:gravity="center"
24 android:textColor="#000"/>
25
26 <ImageView
27 android:layout_width="300dp"
28 android:layout_height="300dp"
29 android:layout_gravity="center"
30 android:layout_marginTop="10dp"
31 android:scaleType="fitXY"
32 android:src="@mipmap/ic_launcher"
33 android:id="@+id/iv"/>
34
35 </LinearLayout>
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.
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.
Compiler will use the classes and methods of the dexter library in this
method.
We will ask for Read and Write external storage permissions in this method.
Compiler will execute the above code when the user clicks on the button.
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.
1 @Override
2 public void onActivityResult(int requestCode, int resultCode, Intent data) {
3
4 super.onActivityResult(requestCode, resultCode, data);
5 if (resultCode == this.RESULT_CANCELED) {
6 return;
7 }
8 if (requestCode == GALLERY) {
9 if (data != null) {
10 Uri contentURI = data.getData();
11 try {
12
13 Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
14 imageView.setImageBitmap(bitmap);
15 uploadImage(bitmap);
16
17 } catch (IOException e) {
18 e.printStackTrace();
19 Toast.makeText(MainActivity.this, "Failed!", Toast.LENGTH_SHORT).show();
20 }
21 }
22 }
23 }
Here, compiler will check if user have selected one image or not.
After that, compiler will set that bitmap into the imageview.
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.
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.