Android Studio Download Image Example




In this tutorial, We like to show you download image in android studio.
Android Studio Download image example

Tool used
1. Android Studio 0.4.0

Task
In this tutorial we will make use of 2 previous tutorials ,Android Progress Bar Example and Android Studio AsyncTask Return Value to Caller. Meaning that we will display a progress bar during the image downloading and once the image downloaded it will notify the caller (activity) to display image in the imageview.

  1. Let’s start to create a project and name it DownloadFiles, and the rest of step should be as standard, if you need guide on this step , please refer this link. These are the basic setting i’ve set.
    Android Studio Download image example

Android Studio Download image example

Note: By default Android Studio include fragment in the project and since we not using it we’ll just remove it 🙂 it’s very easy! Input same name into both layout name and fragment layout name like image above.
  1. In src > main > res > layout > activity_main.xml. Add Button, ImageView, EditText and TextView into activity_main.xml and it will look like this.

  1. In src > main > java > com.instinctcoder.downloadfiles, add a class and named it AsyncDownloadFile.java. Paste below code into AsyncDownloadFile.java

  1. We need to create an interface for us to return back to caller (MainActivity.java) after finish download image in another thread (AsyncDownloadFile.java). In src > main > java > com.instinctcoder.downloadfiles, add a class and named it TaskCompleted.java. Paste below code into TaskCompleted.java

  1. In src > main > java > com.instinctcoder.downloadfiles, update MainActivity.java to look like below.
    The code is quite self-explanatory it’s basically call AsyncDownloadFile class when button is click and then start download image and a progress dialog will display the status of download and once image downloaded it will return to main_activity to display image via onTaskComplete.

That’s all! Click Run (Shift +F10). and should see a screen like below. Input image URL you like to download and wait for the image to display.
Android Studio Download image example
Hope you enjoy the post.




JOIN OUR NEWSLETTER
In case you want to get any future post please allowed us deliver new posts direct into your inbox Or follow us on facebook, google+, tweeter
We hate spam. Your email address will not be sold or shared with anyone else.