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 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 🙂

48 thoughts on “Android Programming Tutorial to Get All Images from Server”

  1. Thanks for this tutorial 🙂
    but there is a problem with me
    json string can’t read in Toast
    the toast appear empty
    any help please

    Reply
  2. Thank you very much for all codes .. reallly appreciated!

    You have a code for showing images in a listView where these images are saved in a folder and you have this code where images are saved in the database itself and you are showing one at a time.

    Can you do a code where you retrieve images from Database and show them in a listView?

    Reply
  3. Dear sir, may I know how can I make it to display all image the moment I open the apps instead of pressing the button to display the image?

    Appreciate and looking forward for ur reply 😀

    Reply
      • Dear sir, sorry for disturb 🙁 i encounter app crash after i click fetch images

        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: FATAL EXCEPTION: main
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: Process: com.example.clementchen.androidimageupload, PID: 20974
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method ‘org.json.JSONObject org.json.JSONArray.getJSONObject(int)’ on a null object reference
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at com.example.clementchen.androidimageupload.ViewAll.showImage(ViewAll.java:77)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at com.example.clementchen.androidimageupload.ViewAll.access$200(ViewAll.java:28)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at com.example.clementchen.androidimageupload.ViewAll$1GetAllImages.onPostExecute(ViewAll.java:118)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at com.example.clementchen.androidimageupload.ViewAll$1GetAllImages.onPostExecute(ViewAll.java:100)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at android.os.AsyncTask.finish(AsyncTask.java:632)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at android.os.AsyncTask.access$600(AsyncTask.java:177)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5832)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        11-29 03:29:10.271 20974-20974/com.example.clementchen.androidimageupload E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

        Reply
        • the main problem is
          java.lang.NullPointerException: Attempt to invoke virtual method ‘org.json.JSONObject org.json.JSONArray.getJSONObject(int)’ on a null object reference

          Reply
  4. 02-05 10:56:07.897 17299-17299/com.example.mundk.showimage E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.example.mundk.showimage.MainActivity.showImage(MainActivity.java:55)
    at com.example.mundk.showimage.MainActivity.access$200(MainActivity.java:24)
    at com.example.mundk.showimage.MainActivity$1GetAllImages.onPostExecute(MainActivity.java:77)
    at com.example.mundk.showimage.MainActivity$1GetAllImages.onPostExecute(MainActivity.java:63)
    at android.os.AsyncTask.finish(AsyncTask.java:631)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)

    Reply
  5. Thanks for a good thing, y know it just amazing job man.. but i need to show in galleryview all images any idea to make possible? any suggesstion?

    Reply
  6. I want to retrieve images from sql server. My images are stored in image datatype. I want to show it on my android app…. which concept should I use. Please guide…

    Reply
  7. I want to retrieve images from sql server. My images are stored in image datatype. I want to show it on my android app…. which concept should I use. Please guide..

    Reply
  8. Hi,

    I have tried the code image is uploaded to database bur when i try to fetch all images it shows downloading but after that there is no image in imageView. Same problem in both the getAllImages and getImage. please tell me solution what should I have to do.
    I had uploaded images to local database.

    Thanks and Regards,
    Avinash Yadav
    avinashsy143@gmail.com

    Reply
  9. ///////In my doInBackground method as follows////

    @Override
    protected Bitmap doInBackground(String… params) {
    URL url = null;
    Bitmap image = null;

    String urlToImage = params[0];

    System.out.println(“XXXXXXXXXXXXXXXXXXXX”+urlToImage);

    try {
    url = new URL(urlToImage);

    image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    System.out.println(“iiiiiiiiiiiiiiiiiiiiii”+image);
    } catch (MalformedURLException e) {
    e.printStackTrace();
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    return image;
    }

    In here I get urlToImage as “http://codex.site88.net/getImages.php?Title=GangaramaTemple” but in image value print as null.
    Please tell me what I want to do fix this?

    Reply
  10. Please sir, i need help everything went fine when i displayed with toast but afterward, my imageview didn’t display the image, but the dialog box was shown downloading image i need help

    Reply
  11. Please Sir,i need your help. I can get the json string in a toast message after pressing fetch all images button but there is no image on the imageView. Please help me!! THANK YOU

    Reply
  12. Can you explain to me the part of the code where you convert the BLOB datatype to ImageView?What is the code on getImage.php is very much required!I tried myself with a tutorial and it is failing.

    Reply
  13. private void showImage(){
    try {
    JSONObject jsonObject = arrayImages.getJSONObject(TRACK); //Error line
    getImage(jsonObject.getString(IMAGE_URL));
    } catch (JSONException e) {
    e.printStackTrace();
    }
    }

    Error is :
    java.lang.NullPointerException: Attempt to invoke virtual method ‘org.json.JSONObject org.json.JSONArray.getJSONObject(int)’ on a null object reference

    Reply
  14. Hello Can help me one case:
    If the database contains a table and table has a column having varbinary image data. How I can generate taht image in android studio after fetching that varbinary image data. I can fetch the varbinary image data through HTTP but can’t use that in android studio to show that image by converting base64 string.
    If you have any suggestion would be very much pleased.

    Reply

Leave a Comment