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 …

android download image

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 🙂

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