Loading...

Downloading File or Image and Displaying in Android

In android, you can easily download any files or images from internet/URL. There are many ways to download images from internet and in this tutorial you will learn one of the easiest ways to download image from internet. Showing download progress derives a good user experience. So in this tutorial, you will also learn to display a progress bar that runs when the app downloads an image or files from the web.

Once the image is completely downloaded, the progress bar is disabled and the downloaded image is shown in an image view.

Related:
Load Image from URL (Internet) in Android
How to Pick an Image From SD Card Gallery and Display in Android App
Display Images in Android GridView

Creating New Android Project

Downloading File or Image and Displaying in Android

Let’s start by creating new android project with com.viralandroid.androiddownloadingfile package name and Android Downloading File as project name.

XML Layout File

Open your main XML layout file and add a button to download file or image when button is clicked. And also add an ImageView to show downloaded image. Don’t forget to give an id to both Button and ImageView. Your XML layout file will look like below.

res/layout/activity_main.xml


Java Activity File

In main activity class, you need to implement necessary classes and buttons. Progress dialog can be shown using ProgressDialog class which is a subclass of normal AlertDialog class.

Also you need to add Async background thread to download file from web/URL. Add asynctask class and name it as DownloadFromURL and extend AsyncTask in your main activity.

After downloading image from internet, read the downloaded image from the sdcard and display it in an image view.

Following is the complete code of java activity file.

src/MainActivity.java


Adding Internet and Writing to SdCard Permission

Before running your project, open your AndroidManifest.xml file and add android.permission.INTERNET and android.permission.WRITE_EXTERNAL_STORAGE. Your AndroidManifest.xml file will look like below.

Downloading File or Image and Displaying in AndroidDownloading File or Image and Displaying in Android



AndroidManifest.xml


Now, run your app by clicking Run button. Make sure you have internet connection to download image or file from web URL. Click the download button; you will see the progress bar with download progress in percentage. After download is completed, the downloaded image is shown in the ImageView, below the download button.
Programming 3171889450160326425
Home item