Android Download Manager with get request - android

I have implemented Download Manager code for download excel file from server, but not able to download.
Uri uri = Uri.parse("http://myip/getfile?param1=value1&param2=value2")
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle(getString(R.string.app_name));
request.setDescription(getString(R.string.msg_wait_for_download));
request.setVisibleInDownloadsUi(true);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
getString(R.string.app_name)+File.separator+"statement_xyz.xls");
long refid = downloadManager.enqueue(request);

Related

There was a problem parsing the package giving error when I downloaded apk file

There was a problem parsing the package giving error when I downloaded apk file
Uri uri= Uri.parse(downloadUrl);
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
DownloadManager.Request.NETWORK_MOBILE);
request.setTitle(title);
request.setDescription(description);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

Download Manager downloads HTML

I am using the default Android DownloadManager in the application. So, when I download a file, there is a session parameter attached in the URL, if the session is invalid or expired the user needs to login.
In my case, when the session is valid, I am able to donwload file. Else it downloads an HTML file.
Can anyone please help me out on this. I have attached the code for same below:
Uri uri = Uri.parse(arrayListDownloadProduct.get(position).getDownload_link() + "&SID=" + CommonMethods.getSessionId(mContext));
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(arrayListDownloadProduct.get(position).getTitle());
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setDescription("Downloading music file..");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference = downloadmanager.enqueue(request);

Image URL is downloaded as .Zip format

I have used DownloadManager class for image download. When I have used below image url in browser it is working fine. but when i have downloaded that image url using DownloadManager it is getting .zip format.
Image Url : Here
Below is my code of download Manager :
private void startDownload(String url) {
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(true);
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
request.setMimeType(mimeString);
request.setTitle(getString(R.string.app_name));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Set a description of this download, to be displayed in notifications (if enabled)
request.setDescription("Downloading " + txtDocName.getText().toString());
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, System.currentTimeMillis() + ".jpeg");
downloadManager.enqueue(request);
AppLog.showD(TAG, "downloadind started");
}
The provided URL returns zip as a type. You can check that using any dev tool on your browser as demonstrated in this screen shot

Image downloaded from DownloadManager queuing on Android

I have this code that downloads an image but for some reason the image is in my downloads as "queued" instead of downloading.
String url = file.public_url;
getContext();
DownloadManager manager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(file.filename);
request.setDescription(file.filename + " description.");
request.allowScanningByMediaScanner();
request.setAllowedOverRoaming(true);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, file.filename + file.file_extension);
manager.enqueue(request);
I have successfully downloaded the images with this Async version that doesn't use DownloadManager https://stackoverflow.com/a/3028660/6136947

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

Categories

Resources