Displaying download speed in Download manager notification - android

Q1. I'm new to android. I'm trying to develop an app which uses download manager. I want to display download speed of the connection (mobile/Wifi whatever connection is the user on and downloading). I don't how to achieve this.
Q2. Is there any issues with the download manager not downloading via local address like 192.168../file_name.extn? only when data is on then the download notification appears but the download doesn't always fails. The server works fine, works on the browser.
The code im using :
String url =mainActivity_downloadLink.getText().toString();
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.setTitle("Testing ");
request.setDescription("the file is downloading");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
, request.setDestinationInExternalFilesDir(MainActivity.this,Environment.DIRECTORY_DOWNLOADS,""+System.currentTimeMillis());
//request.setDestinationInExternalFilesDir(MainActivity.this,Environment.DIRECTORY_DOWNLOADS,URLUtil.guessFileName(url, contentDisposition, mimeType));
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
}
Any help thanks in advance.

Related

Android Download Manager response message

I am trying to download a file using DownloadManager, but I have a few scenarios to make decision on download response.
How to get success response message from download manager?

The Android Download Manager api failed to download ("Unsuccessful download")

In some occasions, when trying to download a file with the Android DownloadManager, the download failed and the download notification shows "Unsuccessful download".
After some hours of debugging, I realized some of the headers that I was passing to the download manager were null or empty. Especially the "User-Agent", when that happens the download manager posts a notification saying "Unsuccessful download".
val androidDownloadManager = applicationContext.getSystemService(DOWNLOAD_SERVICE) as AndroidDownloadManager
val request = Request(Uri.parse(download.url))
request.addRequestHeader("User-Agent", download.userAgent)
androidDownloadManager.enqueue(request)

DownloadManager fails with ERROR_UNKNOWN on API 17

I'm trying to use android's own DownloadManager and it works perfectly on API 18+ but the same code fails (STATUS_FAILED) with reason ERROR_UNKNOWN almost as soon as I enqueue it on API 17 phones. here's my code
Context context = MyApplication.getSharedContext();
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE).setTitle(notiTitle).
setVisibleInDownloadsUi(false);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+fileName);
request.setDestinationUri(Uri.fromFile(file));
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request) ;
After days of struggling with this problem , I accidentally discovered the root of the issue. the urls to downloads which were fetched from a server had "[" and "]" characters. they posed no problem in API 18+ DownloadManager but in API 17 the download fails with ERROR_UNKNOWN with no information as to why whatsoever.
replacing them with %5B and %5D respectively solved the problem.
url = url.replace("[","%5B").replace("]","%5D");

Android DownloadManager/DOWNLOAD_SERVICE: how do you decode content-encoding: gzip?

I can't seem to be able to get DownloadManager in Java/Android properly decode gzip files (in this case from S3 bucket). Same link decodes properly when downloading from, say, Chrome, which seems to use DownloadManager as well. There doesn't seem to be anything in the docs that explain how to do this.
here's some sample code. The result is the file gets downloaded, but is still in gzipped format. Not the case when downloading with chrome. How do I do this properly?
downmanager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse("https://somurl.com/somefile.wad"));
request.addRequestHeader("Accept-Encoding", "gzip, deflate");
Uri localUri = Uri.parse("file://"+this.getExternalFilesDir(null).getPath()+"/test.wad");
request.setDestinationUri(localUri);
downmanager.enqueue(request);

Use DownloadManager to download from an URL accessible through a DefaultHttpClient

In my Android app I have to download files from URLs. I use the Android DownloadManager but the URLs are accessible only through a DefaultHttpClient. Infact these URLs are the result of several POST requests. I mean: I'm allowed to download the files only if I'm logged in as user. Could someone help me in managing this situation? I think that it's not possibile to use the DM to download the files by putting directly the link of the requested resource. When I have to download a file there already is an active DefaultHttpClient and I'm logged in.
The code I use is the following:
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(client.HttpsGet(url));
enqueue = dm.enqueue(request);
The HttpsGet is a method of my custom class HttpsClient that does a HTTP GET. In this case I obtain the file encoded in text (like if I open a pdf with notepad). If I directrly put the url of the file (without calling the HttsGet) the devices downloads a blank file.
For posterity...
I solved my problem thanks to this thread: How do download a file with login using HttpURLConnection
But I had to renounce to the DownloadManager
Hope this helps!

Categories

Resources