How to download file from URL using DownloadManager in Android Studio
MainActivity.java
package com.learnoset.testproject;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Downloading file from URL using DownloadManager
downloadFile("Your file Url here");
}
private void downloadFile(String fileUrl) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileUrl));
// set meta details to the file
request.setTitle("Title for the file");
request.setDescription("Description for the file");
// If you are download big file that takes time then you can pass true in this function so device won't be shut down while file is downloading.
request.setRequiresCharging(true);
// set filename
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "your_file_name.png");
// set notification VISIBILITY
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
// put file to the download queue
final long downloadId = manager.enqueue(request);
}
}
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
How to create Modern Login and Register screen in Android Studio | Material UI Design | Part - 06
Simple Music Player App for Android (Offline Version)
Settings and Profile page UI design in Android Studio with Example and Source Code
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.