Android Programming Tutorial to Get All Images from Server

Hello friends welcome to one more Android Programming Tutorial. In the last couple of Android Development Tutorial posts we have seen uploading image from android gallery to MySQL database. And fetching images from MySQL database to android. In …

android programming tutorial

Hello friends welcome to one more Android Programming Tutorial. In the last couple of Android Development Tutorial posts we have seen uploading image from android gallery to MySQL database. And fetching images from MySQL database to android. In this android app tutorial we will fetch all the images stored in our MySQL database at once.

Before going further in this tutorial you should check the last android programming tutorials.

  1. Android Upload Image to MySQL Database
  2. Android Fetch Image from MySQL Database

If you have already read the last android programming tutorial then lets move ahead.

Lets Start our Android Programming Tutorial

  • We will be using the same database, where we have already uploaded some images. My database is as follows.
android programming tutorial
Database
  • As you can see our database has images.
  • Now we will create a simple php script to get the live URL of all the images in our database.
  • for getting the URL we will use our getImage.php script which we have created in last tutorial.
  • We can get a particular image using our getImage.php, with a simple get request. For example if we want to get image of id 31 we will execute our getImage.php like -> getImage.php?id=31.
  • But in this tutorial we will have to get all the images at once. So we will create a new script to get all the images.
  • Create a php file and copy the following code.

  • As you can see in the above code we have the url of our getImage.php. And inside the loop we are concatenating ids of the image at the end and storing it to an array.
  • The loop will store url of all the images inside our array.
  • And finally we are encoding the array in json format.
  • We will read the json from Android.
  • Save the above file and upload it to your hosting account. In my case I have uploaded it to http://simplifiedcoding.16mb.com/ImageUpload/getAllImages.php
  • If we go to the above given URL we will see the JSON as follows

Creating a new Android Project

  • Create a new Android Project.
  • In activity_main.xml we have to create the following layout.
android programming tutorial layout
Android Programming Tutorial Layout
  • The above layout has three buttons and one image view. You can use the following xml code to create the above layout.

  • Now lets move to your MainActivity.java file

  • As you can see we have implemented View.OnClickListener interface. Created our button and added the listener to the button. We also declared a String having the URL of our getAllImages.php file.
  • Now we need to create the getAllImages() method.
  • This is our getAllImages method.

  • So the above code is very simple, we are using AsyncTask to read our JSON. And after reading we are displaying the json in a toast message. We are displaying json in toast only to check that json have been read or not. We will remove this toast further. But for now lets check whether the json is being read successfully or not.
  • Now add internet permission to your manifest and run the project.

  • If you are getting the json string in a toast message after pressing fetch all images button then your project is working fine and you can move ahead. You should see the following output.
android programming tutorial get json
Android Programming Tutorial Getting JSON

Converting JSON to Images

  • Remove the Toast Message.
  • We need some more components, so declare the following inside your MainActivity.

  • Inside onCreate, initialize them and add listeners to buttons.

  • Now we will create a getImage method. This method will take a string as a parameter. The string would have the url to image extracted from json array.

  • Now we will create two more methods. The first one to extract json and other one to show image.

  • We need to make some little changes in our getAllImages method.
  • Inside onPostExecute we have to call  the extractJSON and showImage() method.

  • Now at last we need to create two more methods to display the next or previous image.

  • Now finally call these methods inside onClick.

  • You can see the final complete code below.

Video Demo of Final Output

You can check this video demonstration of what we have created in this tutorial.

Download Source code of this Android Programming Tutorial

If you are having trouble in creating the app shown in this Android Programming Tutorial. Download the source code from the link given below

[easy_media_download url=”https://dl.dropboxusercontent.com/s/su8raogxy4w3kwf/android-programming-tutorial-get-all-images-from-server.zip?dl=0″ text=”Download Source”]

To display all the images in a List check this tutorial

So thats all for this Android Programming Tutorial friends. If you want to learn more about android application development you can go through the official android developers website. And leave your comments for any kind of queries. 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