Android Upload Image to Server Using PHP MySQL

Hello friends, welcome to our new tutorial, in this android tutorial we will create a simple Android Upload Image to Server App using PHP and MySQL. Follow Updated Tutorial from this Link Android Upload Image using …

android upload image to server

Hello friends, welcome to our new tutorial, in this android tutorial we will create a simple Android Upload Image to Server App using PHP and MySQL.

Follow Updated Tutorial from this Link
Android Upload Image using Android Upload Service

In this tutorial we will upload small image files from android to server. If need to upload larger files like a captured video check this tutorial

Android Upload Video to Server using PHP

If you want to see what exactly we will be creating in this Android Upload Image to Server app then you can see this video demonstration first.

Android Upload Image to Server Video Demonstration

If you liked the demonstration of Android Upload Image to Server App then go ahead to know how you can do this. Lets start creating our Android Upload Image to Server Project.

This Android Upload Image to Server is having two parts

  1. PHP MySQL
  2. Android Studio

Lets start with PHP MySQL for your Android Upload Image to Server app.

Android Upload Image to Server – PHP MySQL Part

  • I am using hostinger hosting as my web server.
  • So go to your hosting accounts PhpMyAdmin and create a new table.
  • I have created the following table
android upload image to server
Android upload image to server – Database
  • Now we need to create a php script for our database connection
  • Create a new file named dbConnect.php and write the following code

  • Now we need one more php script to get and save our image sent from android device
  • Create a new file upload.php and write the following code

  • Now upload both the files to your hosting account
  • We need the address of upload.php file
  • In my case the address of my upload.php is -> http://simplifiedcoding.16mb.com/ImageUpload/upload.php

Android Upload Image to Server – The Android Part

  • Open android studio and create a new android project. (I have created ImageUploadSample)
  • Firstly we will create a new class to handle our Http Request
  • Right click on your package name and create a new java class (I created RequestHandler.java)
  • Write the following code

  • In activity_main.xml we have to create the following layout
android upload image to server
Android Upload Image to Server
  • For creating the above layout you can use the following xml code

  • Now come to MainActivity.java file
  • First we will declare some variables

  • Inside onCreate method we will initialize our components

  • As you can see we have added setOnClickListener to the buttons.
  • So now come to your onClick method and write the following code

  • Here we have two methods showFileChooser() this method will show the gallery from where user can choose image to upload.
  • When the user will choose an Image to upload, then after selection we will show that image to the ImageView we created in our main layout.
  • These two methods will do all the require things

  • Now we have the Image which is to bet uploaded in bitmap.
  • We will convert this bitmap to base64 string
  • So we will create a method to convert this bitmap to base64 string

  • Now finally create uploadImage() method

  • Now thats it for Android Image Upload to Server. You can see the final complete code below for MainActivity.java

So thats it for our Android Upload Image to Server app. In the next part of Android Upload Image to Server app we will see how we can fetch the uploaded images. You can also download the source code of this Android Upload Image to Server app from the link given below.

Android Upload Image to Server – Download Source Code

[wp_ad_camp_1]

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

Next Part –  Download Image from Server using PHP MySQL.

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