Android Download Files & Save - Androhub

Download Files Banner

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. :)

 

95 Comments

Dharmesh Patel
Tuesday, June 30th, 2020

Not working in Android 10(Q) api 29.

Dharmesh Patel
Tuesday, June 30th, 2020

File can’t download in Android 10(Q) API 29.

Dr. Droid
Wednesday, July 1st, 2020

Hi Dharmesh,

The code is written for old devices I think you have to provide runtime permission (https://www.androhub.com/working-android-marshmallow-6-0-permissions/). Let me know if the issue is not related to permissions.

Thanks

Post comment

Your email address will not be published. Required fields are marked *