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 Download Image from MySQL Database

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

android download image

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<?php

if($_SERVER['REQUEST_METHOD']=='GET'){

$id=$_GET['id'];

$sql="select * from images where id = '$id'";

require_once('dbConnect.php');

$r=mysqli_query($con,$sql);

$result=mysqli_fetch_array($r);

header('content-type: image/jpeg');

echobase64_decode($result['image']);

mysqli_close($con);

}else{

echo"Error";

}

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)

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.

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.

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?

I have stored the link not the images that’s the problem..And I don’t have that much time to change everything.My images are stored on biz.nf web hosting server and my database is also stored on it.I want to know what link should I store in database to fetch images from the server.

Hi Belal bro
I am also following your code for uploading and downloading the image from server.I have done uploading successfully but i want download that image also but downloading is not happening. pls if possible help me.

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.

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

Hi sir, i want to see only the codes for fetching images just like what I have watched in your demonstration in youtube. https://www.youtube.com/watch?v=kAQ2iORxl-k
I would take this as a grateful gratitude if you grant my request. This will help me a lot. Thanks. 🙂

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.

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

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.

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.

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.

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.

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.

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.

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

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy

About Simplified Coding

Simplified Coding is a blog for all the students learning programming. We are providing various tutorials related to programming and application development. You can get various nice and simplified tutorials related to programming, app development, graphics designing and animation. We are trying to make these things simplified and entertaining. We are writing text tutorial and creating video and visual tutorials as well. You can check about the admin of the blog here and check out our sitemap