Android Upload Image Using PHP MySQL and Display Images in ListView

Hello friends welcome to our new Android Upload Image Using PHP MySQL Tutorial. I already published tutorials about how to upload image to server from android. But in last tutorial I stored android images to MySQL database. …

Android Upload Image To Server Using PHP MySQL

Hello friends welcome to our new Android Upload Image Using PHP MySQL Tutorial. I already published tutorials about how to upload image to server from android. But in last tutorial I stored android images to MySQL database. Now in this tutorial we won’t store image to MySQL database instead we will save our image inside a directory. So in this tutorial we will store image to a directory and the path of the image to our mysql database. And we will also fetch the stored image to our Android App. So lets begin.

Android Upload Image Using PHP MySQL Video

You can check out this video before moving ahead. This will show you that what you will be creating with this tutorial.

Making Your Server Ready for Image Upload

  • So we also need to create a database table this time. But this time we will store the path to the image in database. Create the following database
android upload image using php mysql
android upload image database
  • Now, in your server create a new directory. I created PhotoUpload
  • Inside the directory you need a script to connect to your database. So create a script name dbConnect.php and write the following code

  • Now you need one more script that will handle the photo upload. So I created a script named upload.php with the following code

  • Now the above script will handle uploads. It will store the images sent to a directory name uploads. So you also need to create a directory name uploads.
  • The above script will store the path to the images inside the MySQL database through which we will fetch all the images.
  • So we need one more image which will give the urls of all the images. For getting the urls we will use JSON.
  • Create a new script named getAllImages.php and write the following code

  • Now our server part is over. You can see below the snapshot of my server’s directory.
Android Upload Image Using PHP MySQL
Android Upload Image To Server Using PHP MySQL
  • I have the path of my upload.php and getAllImages.php script.
  • upload.php -> http://simplifiedcoding.16mb.com/PhotoUpload/upload.php
  • getAllImages.php -> http://simplifiedcoding.16mb.com/PhotoUpload/getAllImages.php

Creating Android Upload Image Using PHP MySQL Project

android upload image to server
Android Upload Image – Main Activity Layout
  • The following xml code will create the above layout

  • Create a new java class to handler our http request. I created RequestHandler.java

  • Now in the MainActivity.java write the following code to make the upload work.

  • Your app will now give you some error because we have not created new activity named ImageListView.class. You can comment this line for now.
  • Now add Internet permission to your manifest. Your upload image will work at this point.
  • Try running your app your image upload should work.
Android Upload Image Using PHP MySQL
Uploading Image
  • Now the lets move to the next part which is downloading uploaded images.

Showing Uploaded Images to a ListView in Android

The process I will be following here is good when you have to load few images. Because here we will be downloading all the images at once. And if you have 100s or 1000s images to show up on ListView, you should not follow this. So to make your list efficient check this tutorial here

Android Adding Items to List on Scroll

  • Uncomment your line which were causing error before. Now to remove this error we need to create a new Activity.
  • Create a blank activity. I just created ImageListView.
  • Now when we will click the View Image button this activity should open.
  • In this activity we will create a ListView. So come to the respective layout of this activity. (In my case it is activity_image_list_view.xml
  • We need to create the following layout
Android Upload Image Using PHP MySQL
Android Upload Image Using PHP MySQL
  • You can use the following xml code for creating above layout

  • Now we need to fetch all the Bitmaps from server. So for this we will create a new class. I created GetAlImages.java

  • We will pass the json string having all the urls of our images to the constructor of this class.
  • We will get the json string having all the urls from our getAllImages.php script.
  • Now come to ImageListView.java class and write the following code

  • Now we will create a new activity to see the full size image. I created ViewFullImage.java.
  • We will create a ImageView to the layout file of this activity.
Android Upload Image Using PHP MySQL
Android Fetch Image from Server
  • The xml code for the above layout is

  • Inside your java class for this layout write the following code

  • Now thats it. If you want the source code of this project you can download from here

[easy_media_download url=”https://dl.dropboxusercontent.com/s/697f4y7ah9x0wm0/android-upload-image-to-server-using-php-mysql.zip?dl=0″ text=”Download Source”]

So thats it for this Android Upload Image Using PHP MySQL Tutorial friends. If you are having confusions please leave you comments below.

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