I want to download a binary file from a url. Is it possible to use the Android download manager class that I found here DownloadManager class?
Make sure the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions is in your Manifest.xml file:
And then include this code in your download function
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
// this will request for permission when user has not granted permission for the app
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
else{
//Download Script
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("URL of file to download");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setVisibleInDownloadsUi(true); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());
downloadManager.enqueue(request);
}