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

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);

Related

Displaying download speed in Download manager notification

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.

Content-Disposition response header in DownloadManager

I have to download a file and was using downloadManager for it. The filename for the file resides in the Content-Disposition of the response header.
Is there anyway of accessing the response header from DownloadManager.Request class or downloadManager could support this out of the box.
I don't want to make a HEAD api call to the server and increase the server hit count. We can assume we don't have HEAD call functionality.
You maybe have a look to OkHttp3 instead.
implementation 'com.squareup.okhttp3:okhttp:3.14.1'

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");

How to set uploaded jpg type to be recognised as "image/jpg" instead of "application/octet-stream"

I am using Loopj's android-async-http This is my code:
File file = new File("sdcard/food.jpg");
params.put("image", file);
My problem is, php server received the images as application/octet-stream instead of image/jpg. I need it to be image/jpg as my server guy white-list uploaded files that have types of image/jpg, image/png, etc.
UPDATE 1
I've tried adding content type parameter to the application. However, php code still recognized it as application/octet-stream.
params.put("image", new FileInputStream(file), "image/jpg");
UPDATE 2
Since I need to move on with the project, what we do right now is propose server guy to implement mime sniffing by using exif. In php, we do it with exif_imagetype(). All is well, so far. I will keep this question open until we find the solution that is implementable in android.
I have faced the same problem, yet I couldn't change application/octet-stream but I instead handle it on server:
$info = getimagesize($_FILES['pic']['tmp_name']);
$type=$info['mime'];
here the type will be shown as image/jpg instead of application/octet-stream.

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