Android Download Manager Doesn't Work on Private Network - android

I use firewall on my network and this network has just one Ip that allowed access. I want to download my apk file from special Ip but Download Manager adds to list as STATUS_PENDING. How can I download this file without VPN?
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(urlPath));
downloadRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
downloadRequest.setAllowedOverRoaming(true);
downloadRequest.setAllowedOverMetered(true);
downloadRequest.setTitle("NetworkApp");
downloadRequest.setDescription("Downloading NetworkApp " + versionName);
downloadRequest.setVisibleInDownloadsUi(true);
downloadRequest.setDestinationInExternalFilesDir(context, null, "NetworkApp.apk");
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
long downloadId = downloadManager.enqueue(downloadRequest);
Android Api:28

Related

How to detect the extension of file to download using DownloadManager in Android?

I start developing my very first Android project. In my project, I need to download media files, especially mp3 or mp4. I am downloading file using DownloadManager.
Here is my download code for mp3
private void downloadPodcast(int id)
{
String url = context.getResources().getString(R.string.api_endpoint)+"podcast/download?id="+String.valueOf(id);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading...");
request.setTitle("Podcast");
request.setMimeType("audio/MP3");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "audio.mp3");
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
As you can see in my code, I am downloading only mp3 and setting the MIME type is constant. The file name and its extension is constant as well. What I want to do is to detect the file extension I will download. So can I set the MIME type programmatically and set the extension of file name. How can I achieve it in DownloadManager?
Though its quite late, however here is the answer:
You can get the extension of the file to download using the code below and add the extension to your file name.
String fileUrl = "http://someurl";
String fileName = "foobar";
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
// concatinate above fileExtension to fileName
fileName += "." + fileExtension;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileUrl))
.setTitle(context.getString(R.string.app_name))
.setDescription("Downloading " + fileName)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);

Download Manager in Android

I'm developing a magazine app and I need to download one edition of the magazine to be available in offline mode. One edition may have 20 articles.
I'm using download manager to download the articles, enqueueing all of them, but I need a callback when finished to download all articles of one edition.
If I click to download two magazines at once, it'll enqueue all the articles of both magazines and I need that they be individual.
So far I have this:
mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
for (final Materia materia : materiasList) {
String url = materia.url;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Baixando...");
request.setTitle(mEditionTitle + " - " + materia.titulo);
request.allowScanningByMediaScanner();
request.setDestinationInExternalFilesDir(mContext, "/editions/" + String.valueOf(mEditionId) + File.separator, String.valueOf(materia.id) + ".html");
// get download service and enqueue file
mDownloadManager.enqueue(request);
}
EDIT:
I'm running this manager from a service.
Not sure this is the best solution but you could query your download manager at regular interval and get all the in progress downloads. Then you parse the result and check if it is still downloading some items for a collection or if they are all downloaded.
Query the manager:
ArrayList<QueuedDownload> downloads = new ArrayList<>();
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query myDownloadQuery = new DownloadManager.Query();
myDownloadQuery.setFilterByStatus(DownloadManager.STATUS_RUNNING | DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING);
Cursor cursor = downloadManager.query(myDownloadQuery);
while (cursor.moveToNext()) {
// Parse each download
}
cursor.close();
Of course that should run on a background thread.

Download file via inbuilt download Service

I am trying to download a file from remote server in my app. I do not want to right custom code for this. I want to download the file via inbuilt downloader(which android has built in). How to do that? And is it the correct option for this:
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png"));
enqueue = dm.enqueue(request);
try this code it's really working...
DownloadManager mgr = (DownloadManager) context.getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png");
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir("/test_folder", "testimage");
mgr.enqueue(request);

How to replace files in same location using Download manager in Android?

In my app I need to download some files(images,Pdf&txt). I am successful to save files through download manager in Downloads folder, But I want if file already exist in downloads folder and user again click on same file then it delete older one in download folder and replace with new file. Please let me know how it is possible?
My code:
database = new DMS_Database(Files_Folders_Activity.this);
if(!database.dididExist((doc_Id))) // if file does not exist then it will save file in the database and in downloads folder.
{
database.Save_Doc(doc_Id, name, url); //Database method
System.out.println("you are here: Save Files");
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(name);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/Downloads", name);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
else
{
database.update_Doc(Doc_Id,name,url);
//But if file already exist then How to delete file and again save on same location. I don't want duplicate file. Right now it is creating two websites also not updating database.
System.out.println("you are here: update files");
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(name);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/Downloads", name);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
database.close();
Please let mek now ow it is possible, thanks.

Android: Download an application using the DownloadManager class

I am trying to download a non-market application in Android using the DownloadManager class.
what I am doing is the following:
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse("PATH_TO_MY_APP"));
long enqueue = dm.enqueue(request);
The notification bar shows me that the app is being downloaded. But I am not able to install it or to find it on the device. What I am doing wrong?
Same problem. Solved with a call to:
public DownloadManager.Request setDestinationUri (Uri uri)
You need to have WRITE_EXTERNAL_STORAGE permission.
Uri src_uri = Uri.parse("http://your.url.here/File.apk");
Uri dst_uri = Uri.parse("file:///mnt/sdcard/download/File.apk");
DownloadManager.Request req = new DownloadManager.Request(src_uri);
req.setDestinationUri(dst_uri);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(req);

Categories

Resources