Android Download Image from Server using PHP and MySQL

In the last post we uploaded our image from gallery to our MySQL Database. In this post we will see how we can fetch those uploaded images. If you have not read the last tutorial, then before going through this Android Download Image from Server tutorial you should first check the last tutorial.

Android Upload Image to Server Using PHP MySQL

Android Download Image from MySQL Database

  • First go to your hosting account. For this tutorial I am using free hosting account by hostinger.
  • In last tutorial we created two scripts. One for database connection and other one for storing image to database. Now we need to create one more php script that will fetch the uploaded image.
  • Create a new PHP file. I am creating getImage.php
  • Copy the following code

Explanation of Code:

  • As you can see we are receiving the id with Http Get Request. Then we are fetching the particular image string using sql query. Now set content-type as image or jpeg using header function. And finally we just written an echo statement with base64_decode function. This will echo the image string in image format.

Now go ahead:

  • Upload this file to your hosting account.
  • Note down the URL for this php script. In my case the URL is – http://simplifiedcoding.16mb.com/ImageUpload/getImage.php

Creating Android Download Image Activity

  • Now we will create a new Activity. In this activity user will give the image id and we will fetch the image by id.
  • Right click on layout-> New -> Activity -> Blank Activity (I created ViewImage)
  • Now a layout xml file and a java file is created (ViewImage.java and activity_view_image.xml)
  • Come to the layout file. We need to create the following layout.
android download image
android download image
  • Use the following code for creating the above layout

Explanation:

  • We have created a horizontal linear layout inside a vertical linear layout. In horizontal part we have an EditText and a Button. In EditText user is supposed to enter the image id. After entering image id user will press the button and then we will show the fetched image in the ImageView we created.

Go Ahead:

  • At last tutorial in our main layout we created a View Image button. Now we will add functionality to this button.
  • So come to your MainActivity.java class
  • See this code this is the updated code. You need to update your code as shown here.

Explanation:

  • This part is same as the last part. We just created a new button to open ImageView activity. We are opening the ImageView activity by using Intent.

Go Ahead:

  • After updating your MainActivity.java file, when we will tap the view image button, our ViewImage activity will open.
  • Now finally come to your ViewImage.java file and write the following code

Explanation: 

  • We declared the instances of our EditText, ImageView, Button and RequestHandler. In onCreate method we initialized our instances and for button we set the onClickListener which is the current class itself.
  • In onClick method we wrote a method getImage(). This method will fetch the respective image according to the id given.
  • Now we need to create the getImage method. See the following code.

getImage()

Final Code for ViewImage.java 

  • Now thats it for Android Download Image tutorial.
  • You can also download the source code of Android Download Image App from the link given below.

[easy_media_download url=”https://dl.dropboxusercontent.com/s/7eg2d1zq5brev1y/android-upload-image-to-server.zip?dl=0″ text=”Download Source”]

Video Demo of Android Download Image from MySQL Database

So thats it for this tutorial friends. If you are having any confusion for this Android Download Image Tutorial feel free to leave your comments. Thank You 🙂

89 thoughts on “Android Download Image from Server using PHP and MySQL”

  1. Hi Belal Khan, I try my way to retrieve image file and store them into array along with other data type such as name, username, etc and parse them to JSON, my JSON now contains base64 string image, is this a good practice? MY JSON looks like this {“id”:”1,”name”:”cw fei”,”image”:/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABsSFBcUERsXFhceHBsgKEIrKCUlKFE6PTBCYFVlZF9VXVtqeJmBanGQc1tdhbWGkJ6jq62rZ4C8ybqmx5moq6T/2wBDARweHigjKE4rK06kbl1upKSkpKSkpKSkpKSkpKSkpKSkpKSkp…..”}

    I found it will be lag if obtain multiple images at once, so should I follow your way to obtain image one by one?

    Reply
  2. Hello,

    I have followed your tutorial here is my problem when i enter a id number in the search field then nothing happend,
    i have stored a picture in my database (LONGBLOB).

    can you please help me..

    i will gladly email you my code

    Reply
  3. Hello Again,

    i found out what the problem whas,

    when i uploaded from the app then the search field Works, but if i upload image directly from MYSQL then it does not Work.

    can you please tell me how i can create onclik on the image and then get a POPUP box there will show data from MYSQL database ?

    Reply
  4. I have stored the name of image in database and uploaded the image on web server so how do I get the lnik of image to display in the application

    Reply
  5. I copied all your code for image uploading to server and also same table with same names but it does not work.O am using xamp as server.server. Plz suggest me what to do i have an important project for which image uploading is a part.it is due soon.

    Reply
  6. hi sir
    i have json data with image
    {“Response”:[{“name”:”raj”,”mobile”:”9931675657″,”image”:”image\/computer.jpg”},{“name”:”raj”,”mobile”:”8251843109″,”image”:”image\/ecbranch.jpg”},{“name”:”raj”,”mobile”:”9534979772″,”image”:”image\/bbb.png”}]}

    and i want to fetch data with image by giving name and show in listview . plse sir help me how can i do this or post the code for this

    Reply
  7. how to store some images on mysql database manually and then fetching it and displaying it in a grid view in android studio? please give me some tutorials or code.

    Reply
  8. Hey..
    I have problem .
    I can’t retrieve image from server .
    I debug my code in that I can’t receive any image . In that i receive null .
    please help me out .

    Reply
  9. hey ilove you tutorial, can you create an application in android that display image in mysql and populate list view with it

    Reply
  10. Is it possible to click on an item XYZ of the listViewItem not to see the item XYZ in more detail, but to connect to another mysql-table so to show in a new listview all the rows referring to the item XYZ?

    I’ve succesfully applied your instructions to create the first customlistview (Demand), and the second listview (Supplies). Both display correctly starting from the MainActivity, but I cannot start the second clicking on an item of the first.

    Thank you for your reply

    Reply
  11. Uploading is working fine,but when i enter id to view image I am not getting image.I have followed your tutorial , my problem when i enter id in the search field then n I am not getting image,

    Reply
  12. hey !! First I tried to upload image using your code and it worked great but now when I am trying to fetch the image using your code its not working . It is not showing any error but i am not getting the image on imageview, plzz help me ..
    thanks in advance

    Reply
  13. the uploading part works fine but im not able to view images back from the server. i tried debugging . the doinbackground method returns null in the image.

    Reply
  14. Hi Belal Khan, Thank you very much for sharing your knowledge, I have a question I want the button is in an activity and that giving click send him to another activity in which the image is displayed. your collaboration? Thanks.

    Reply
  15. Hello sir.. I have a doubt regarding displaying image in android app based on range of data stored in a table. I mean we have to display different image for different range of data in table. Iam using Android Version 5.0 and Wamp server for database connectivity. Please clarify my doubt. Thanks in advance.

    Reply
  16. Hello,
    I m Vaibhav. I have successfully uploaded some* images and fetched also. But when i try to upload an image with size of only 65KB or even higher ex. 252KB it is uploading it successfully but it is not fetching those images. Moreover when i choose these images from gallery i even don’t get preview for it. I have also experimented changing the blob with other types such as longblob, mediumblob but it is not helping. Sometimes the app is crashing when trying to upload an image with size of 400KB. Please help on this.

    Reply
  17. Hi, may i know is that “http….?id+”+id this url is only works for the image that you uploaded to server, but what if you input that image directly to mysql database, what is the way to retrieve it? Any help will be appreciated.

    Reply
  18. hiii Belal Khan, i use this code but the image does not shown it shows the ERROR SkImageDecoder::Factory returned null I don’t no why
    .

    Reply
  19. I think u dont need to set the header in the php since we are echoing the image as a base64 string.This was causing me problems in my retrieval of the image

    Reply
  20. Solution: for those where none error comes,in my case i change my image type in table of database from BLOB to Large Blob.If it helps please reply

    Reply
  21. for those image return null:

    //call a function to get “echo [‘image’] (base64 raw data)”
    BufferedReader bufReader = new BufferedReader(new InputStreamReader(inputStream, “utf-8”), 8);
    StringBuilder builder = new StringBuilder();
    String line = null;
    while ((line = bufReader.readLine()) != null) {
    builder.append(line + “\n”);
    }
    inputStream.close();
    result = builder.toString();
    return result;

    //after return result

    byte[] decodestring = Base64.decode(returned result, Base64.DEFAULT);
    image = BitmapFactory.decodeByteArray(decodestring, 0, decodestring.length);

    at least I solved it.

    Reply
  22. You have made great series but in my case image should be fetched when the app starts and can also be display offline without downloading, it is user based app. Image should be displayed in grid view and also be swipe using some page curl view /flip view.

    Please recommend me some from how to implement it.

    Thank you

    Reply
  23. image = null ?

    delete this header code from php file.

    ====header(‘content-type: image/jpeg’);=====

    i dont understand why your tutorial always have bug and we cant download the project in your link (badlink).
    if everything running well i think this is can be the best and simple tutorial for begginer.

    Reply
  24. in database change image datatype blob to long bob…. after that insert new image by app… after this retrieve a image easily… it does not show null..

    Reply
  25. The uploading part executed successfully. But while performing the fetching part i got thus error

    “D/skia: — decoder->decode returned false”

    Please help me with it

    Reply
  26. i also have the same problem here, when upload the image into database it work fine but when trying to download the image it give no error and the image is not display.your tutorial is perfect if you can help to correct this error.
    thanks

    Reply

Leave a Comment