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.


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

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 class and write all download urls here.

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

6. Now create a 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

7. Now create 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 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


About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.


Not working. Shows IO Execption.


It worked, Thanks. But if I want to make it generic for creation of file with extension then how i we do it. How can I get extension from url for create new file of the same name.


It is possible to show the percentage of download for each file?


Hi, Thank you for your nice Tutorial. I have a problem. When I click on download buttons, show Download failed and download again. I set Permissions in Android Manifest.


Thanks. It worked. Downloaded files are store in internal storage but when i click on the downloaded folder to open all files are faded and i am unable to click on them.

    Hi Zoraiz,

    Fading issue is basically may be your device issue or may be you don’t have permission to access those files. Please try to change the location.



Error in code. You are trying to update UI from doInBackground. Try to use RunonUiThread to show Toast.


Dr Droid, Thanks so much, this was very useful.

@Kishor Singh, he is passing a context from the calling function which is being used while showing the Toast message, so there are no errors at runtime.

For those people who are seeing grayed out files here is what I did:
1. Modify the code to store the files in a Public Directory like ‘Download’ (in case you are ok sharing the file with other Apps) using the following call to get the location of the public directory:
2. In phones like Nexus, browse the file using inbuilt application as follows: “Settings->Storage and USB->Explore->Download”. You will see the file without being grayed out.
Hope this helps.

Dr Droid, thanks once again for this wonderful website !!!


Thanks a lot, worked however I have noticed it’s failing to download bigger files. It’s working on small size files less than 1mb but tried 3mb files and failed.


Thanks a lot! Amazing tutorial. It seems that adding request permissions in the manifest file isn’t enough to write in my external memory so i just wanted to add that I had to make a modification in the onClick method of the MainActivity Class before the switch case:

//Before starting any download check if write permissions are granted
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},


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)