Android Upload Image using Android Upload Service

Hello guys, in this post I came up with an easy solution for uploading files from android to server. So today we will see an example of Android Upload Image to Server. I have already …

android upload image

Hello guys, in this post I came up with an easy solution for uploading files from android to server. So today we will see an example of Android Upload Image to Server. I have already posted some example of Android Upload Image to Server previously.  But in this post we will use android upload service for our Android Upload Image App.

Check the Updated Android Upload File Tutorial Here

In this tutorial I will store the image file inside servers directory and in database I will store the URL of the image. For this I will use Android Upload Service library and it makes uploading files super easy. So lets see how we can do it. First we will create our server side codes.

Android Upload Image to Server Video Tutorial

  • You can also go through this video tutorial to learn how to upload image from android to server.

Creating Server Side Codes for Android Upload Image

The first thing we need is to create our server side web services. For server side I am using PHP and MySQL. And for this I am using Wamp server. You can still use xampp or any other application. Now follow the steps to create your web service to handle the file upload.

  • Go to localhost/phpmyadmin and create the following database table.

android upload image database.jpg

  • Now inside your server’s root directory (c:/wamp/www) and create a new folder. I created AndroidUploadImage.
  • Inside the folder create a folder named uploads, in this folder we will save all the uploaded images.
  • Create a file named dbDetails.php and write the following code.

  • Now create a file named upload.php and write the following code.

  • Now test your script. You can use a rest client to test it I am using POSTMAN. See the below screenshot.

android upload image web service

  • If you are seeing the above response then. Your script is working fine. You can check the database and upload folder which you have created.
android upload image database
Database
android upload image database
Upload Directory
  • So its working absolutely fine. Now lets move ahead and create a android project.

Creating Android Studio Project

We are done with the server side coding for this Android Upload Image Example. Now lets code our android application. Follow the steps given.

  • Create a Android Studio Project.
  • Create a class named Constants.java and write the following code. The following class contains the path to our php file which we created.  You are seeing two strings. The second it the path to the file we will create at the end of this post.

  • You need to change the IP according to your system. To know the IP you can use IPCONFIG command in command prompt (windows user).
  • Now we need to add android upload service to our project.

Adding Android Upload Service

  • Go to your app level build.gradle file and add the following line inside dependencies block and sync your project.

  • Come to activity_main.xml and write the following xml code.

  • The above code will generate the following layout.

android upload image example

  • As you can see we have two buttons, one to select image and other to upload image. We also have an EditText to enter the name for the image.
  • Now come to MainActivity.java and write the following code.

  • Finally add the storage and internet permission on your manifest.

  • Thats it now just run your app.

android upload image example app

  • Bingo! Its working absolutely fine. Now you may need to fetch the uploaded images.

Fetching the Uploaded Images

  • To fetch the images inside your server side project, create one more file named getImages.php and write the following code.

  • This code will give you the following JSON.

  • Now you can use the following tutorial to display the images using the above JSON.

Android Custom GridView with Images and Texts using Volley

  • You can also get my source code from the link given below.

[sociallocker id=1372] [download id=”2974″] [/sociallocker]

So thats it for this Android Upload Image tutorial friends. You can also use this method to upload video and any type of file to your server. Leave your comments if having any doubts or feedbacks. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x