Download files in android using Download Manager

In this tutorial we are going to learn how to Download files in android using Download Manager, Display download progress in notification bar and open file by clicking it in notification bar. In android download manager is a system service that handles long-running HTTP downloads. It conducts downloads in the background taking care of HTTP interaction and retrying download after failure. More about download manager can be found here

  • In this application we shall create an application that has a download button.
  • When that button is clicked it will download a simple pdf file i have named it test.pdf.
  • Notification bar shows download progress also shows if file download is complete.
  • When the file is clicked it will open.
  • Download files in android using Download Manager

  • Let’s get started with coding.
  • Creating a new android studio project

  • Open your android studio click File => New Project and give it a name you can call it Download Manager.
  • Click next and select Empty activity and then finish.
  • Adding Permission

  • Add the following permission to your manifest file.
  • This permissions allow us to Write external Storage, Read External Storage and Even work over the NETWORK.
  • Creating the Layout

  • Open your activity_main file located under res => layout => activity_main.xml.
  • Copy and paste the code below to create download layout.
  • The output of the layout above is show below.
  • Download files in android using Download Manager

    Handling download

  • Open your MainActivity file, Here we are going to perform download when the btnDownload is clicked.
  • We are also going to check if permission are granted.
  • The download manager requires file name and the URL of the file which we are going to pass in the downloadFile method
  • Now you are done, Run your application and you will see the following output.
  • Android Download Manager

  • When download button is clicked download starts and is shown in notification bar.
  • When user clicks the file in notification bar the file opens.
  • Click the link here to download the source code
  • If you have a question or need help please comment below.