All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Download File From URL In Android Programmatically

Last Updated : Mar 11, 2024

How To Download File From URL In Android Programmatically

In this article we will show you the solution of how to download file from url in android programmatically, a file is retrieved from a web server and saved to the local storage of an Android device using code.

This procedure is known as downloading a file from a URL in Android programmatically.

In order to complete this task, either conventional Java APIs or APIs tailored to Android might be used.

In order to download a file from a URL on Android programmatically, one normally needs to use the HttpURLConnection and InputStream class to connect to the server, download the file's contents, and then write the entire content to a local file on the device's storage.

Together with maintaining the network connection and looking for faults, the procedure also entails handling several kinds of exceptions that could arise during the download.

Buffering is what boosts performance. Each method call to the read() method when reading a byte at a time implies a system call to an underlying file system.

The application execution context shifts from user mode into kernel mode & back when the JVM calls the read() system function.

This context switch costs a lot in terms of performance. Due to the numerous context shifts required when reading a lot of bytes, your application performance would be subpar.

Long HTTP downloads are managed by the download manager, a system service. Clients can request that a URI be downloaded to a certain destination file.

Step By Step Guide On How To Download File From URL In Android Programmatically :-

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
    Button button;
    DownloadManager manager;
    protected void onCreate(Bundle savedInstanceState) {
        button = findViewById(;
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                Uri uri = Uri.parse("");
                DownloadManager.Request request = new DownloadManager.Request(uri);
                long reference = manager.enqueue(request);


<?xml version="1.0" encoding="utf-8"?>
        android:text="Download Content" />
  1. First we create an import function.
  2. Then we create a class as MainActivity.
  3. Then we create a button.
  4. After that we override a function to create a bundle of saved instances.
  5. Then we create a button for clicking the button as button.setOnClickListener.
  6. After that we create xml code for linearlayout.
  7. Under xml code we create height, width, gravity, orientation.
  8. Then we close the program.

Conclusion :-

The download manager, which also manages HTTP transactions and resumes failed downloads in addition to those halted by connectivity changes and system reboots, will perform the download in the background.

Apps that request downloads using this API should configure a broadcast receiver again for events ACTION NOTIFICATION CLICKED such that they may react appropriately whenever a user clicks on a running download from of the downloads UI or via a notice.

I hope this article on how to download file from url in android programmatically helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪