Android Upload Image Using Retrofit Library Part 1 || Upload Image to Server

In this blog on Android Upload Image Using Retrofit Library we will discuss hoe to upload a image onto server using retrofit library.

Every now and then we require to upload a image to server either it is regarding a user profile picture or any other image from your device to database. Also you can capture a new image and upload it to database.

In general you may be having a view how social media works like facebook twitter, instagram, whatsapp and many more apps provide user a facility to upload their images to their respective accounts right we will see what happens in background for the image upload.

Uploading data to server is explained clearly right from basic database creation.We are using local server in this tutorial for uploading the image name and image to database the same will be applicable for real time server too.

 

Android Upload Image Using Retrofit Library

Creating a Database

Let’s start by creating database first

 

Name of the database : imagetable

Name of the table : imagetable

CREATE TABLE `imagetable` (
 `id` int(2) NOT NULL AUTO_INCREMENT,
 `img_name` varchar(50) NOT NULL,
 `img_path` varchar(150) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=28 DEFAULT CHARSET=latin1

 

Server Configuration

 

config.php

I am using local server (Wamp Server) for this tutorial.

Regarding username and password default’s are “root” and empty field for password you can replace your credentials.

 

<?php

//Define your host here.
$Name = "localhost";

//Define your database username here.
$User = "root";

//Define your database password here.
$Pass = "";

//Define your database name here.
$DB = "imageupload";

?>

 

upload.php

 

Include the config file to connect to database and the insert data into table.

include 'config.php' ;

 

Using this file we are decoding the image and saving it into a folder using the title provided by the user.

file_put_contents($path,base64_decode($image));

 

Insert title, image path in your database

$sql = "insert into imagetable(img_name,img_path) values('$image_name','$path');";

 

Path where image is stored in your database

$path = "Uploads/$image_name.jpg";

 

Close the database

mysqli_close($con);

 

<?php

include 'config.php' ;

$con = mysqli_connect($Name,$User,$Pass,$DB);

$image_name = $_POST['image_name'];
$image = $_POST['image'];

$path = "Uploads/$image_name.jpg";

$sql = "insert into imagetable(img_name,img_path) values('$image_name','$path');";

if(mysqli_query($con,$sql))
{
file_put_contents($path,base64_decode($image));
echo json_encode(array('response'=>"Successfully Uploaded..."));
}
else
{
echo json_encode(array('response'=>"Failed..."));
}
mysqli_close($con);

?>

 

This is it for the Android Upload Image Using Retrofit Library tutorial server side  server side implementations and client side implementation is explained in our next tutorial.

Share like and subscribe us for more interesting tutorials.

 

Output :

The expected output for Android Upload Image Using Retrofit Library

Android Upload Image Using Retrofit Library

 

Android Upload Image Using Retrofit Library Part 2 || Upload Image to Server

 

If you have any query on Android Upload Image Using Retrofit Library do let us know in the comment section below. If you like this tutorial do like and share us for more interesting updates.

 

Show Buttons
Hide Buttons
Read previous post:
Fetch Data Using Retrofit Library Android || GET || Retrofit Library

In continuation to the previous tutorial Android Post Data Using Retrofit  Fetch Data Using Retrofit Library we will be fetching data...

Close