Android Download Files & Save

Android Download Files & Save

In this tutorial we are going to learn how to download pdf, doc , video, mp3, zip ,etc. files from server and save them in device memory.

Below are certain things that we are going to learn in this tutorial :

 1. Download Files – Download Files from Server Url.

2. Save Files – Create Folder in device and save the dowloaded files.

3. Download Multiple Files – Download Many Files at a time.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Open res=>values=strings.xml and add the following strings to it.

3. Create a xml layout naming activity_main.xml and add the following code. In this  layout there are some Buttons for showing downloading different type of files.

4. Create a Utils.java class and write all download urls here.

5. Create a CheckForSDCard.java class for checking if SDCard is present or not.

6. Now create a DownloadTask.java and add the following code. In this code AsyncTask is used for downloading data from server. There are several main steps in doInBackground(Void…arg0) that are listed below :

1. Converting download url into URL:

2. Opening Url Connection for data downloading:

3. Setting request method to server:

4. Connect the open connection:

5. Create a directory in SD Card if not present:

6. Create an Output file i.e. download file inside above created directory:

Note: Give extension to your outputFile like .pdf, .mp3, .mp4 etc.

7. Download File and write it over the outputFile created:

8. Close all connection after downloading:

Full Code of DownloadTask.java

7. Now create MainActivity.java and add the following code. In this code i am starting AsyncTask on button click.
But before downloading it is necessary there should be a proper internet connection. So for this I had created a method naming isInternetConnected() that will return true or false.

If you are new in how to detect internet connection then check Detect Internet Connection Tutorial.

Last Button is for Opening  downloaded folder and the code for opening download folder is below:

Full MainActivity.java Code

8. Finally come to AndroidManifest.xml file and add below several permissions to Manifest file.

9. Finally, all done you can use above steps for downloading any kind of files.

Thanks. :)

Download Source Code – >

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)