Download file via inbuilt download Service - android

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

Related

my pdf document is not getting saved on my phone

i am trying to save a PDFDocument on my phone but i am not able too. this is my code;
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),"/" +nameGet1 + "-" + idGet1+".pdf");
Try using this code, and make use you have permission to save on phone
download.setOnClickListener(v -> {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(your_file_url));
request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(your_file_url));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, your_file_name);
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
});

How to prevent application file deleted when uninstall the app in android

DownloadManager mgr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(file_url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setTitle("")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/MyApp", tsLong.toString() + ".jpg");
The path to download file is here:
new File(Environment.DIRECTORY_PICTURES + "/MyApp")
I found that once the app is uninstall the folder is also deleted, how to preserve that ?
thanks a lot.

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

How to save downloaded file on sd card from download manager programmatically on android

In my application i have download(Image) feature which is used to download file from urls. The download happen should be shown in notification bar so that i used Download Manager class to download file. This is working fine but the downloaded image does not stored no where in the sdcard.
i have referred the url for the download manager.
my requirement is i need to save the download image to sdcard with notification bar indication. What to modify on the code to get save image on sdcard on the above link
i have some doubts regards the code in the above link is Can i use the same code to download audio or video file?
please help me.
Edited question:
I have tried
filepath = Environment.getExternalStorageDirectory().getPath()+"/download/cm.png";
Uri destinationUri = Uri.parse(filepath);
request.setDestinationUri(destinationUri);
before the preference manger on the button click. but i could not get the file on sdcard.
This is what i used.
Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setDescription("Downloading a file");
long id = downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("File Downloading...")
.setDescription("Image File Download")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "cm.png"));
In the code you refer to, the file is opened at the end. At this point, you can consider copying it to the SDCard.
Otherwise (better) use http://developer.android.com/reference/android/app/DownloadManager.Request.html setDestinationUri(android.net.Uri) to specify where you want to download the file.
downloadmanager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Toast.makeText(context, "Downloading...", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("---- url here ------");
request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle("---- title here ------");
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl("---- url here ------");
request.setMimeType(mimeType);
request.setDescription("---- descripation here ------");
if("---- titlehere ------" != null){
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "---- title here ------");
}
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference = downloadmanager.enqueue(request);

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