I have used Android Download Manager for download Files from server as it's handles network error and resume downloading.
It's working fine, I can download files using this,
Problem : When I start downloading it's complete successfully and file is also downloaded once but it Store/Show the downloaded files in two directories one is default download directory of android phone and second is custom directory which created by me as i only want to Store/Show in my custom directory and then want to read only from that directory files.
Question : how i only Store/Show downloaded files in custom created directory ??
Below is the code which i used to download files using Download Manager.
public void downloadWithManager(String urlPath) {
Long tsLong = System.currentTimeMillis() / 1000;
File direct = new File(Environment.getExternalStorageDirectory() + "/Demo");
if (!direct.exists()) {
direct.mkdirs();
}
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(urlPath);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
//Restrict the types of networks over which this download may proceed.
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
//Set whether this download may proceed over a roaming connection.
request.setAllowedOverRoaming(false);
//Set the title of this download, to be displayed in notifications (if enabled).
request.setTitle(tsLong.toString() + ".mp4");
//Set a description of this download, to be displayed in notifications (if enabled)
request.setDescription("Download in progress...");
//Set the custom local destination for the downloaded file
request.setDestinationInExternalPublicDir("/Demo", tsLong.toString() + ".mp4");
//Enqueue a new download and same the referenceId
downloadReference = downloadManager.enqueue(request);
}
Thank you.
Related
I am using DownloadManager to download pdf file in my application. In devices with OS version of Oreo and above, the file is queued for download and keeps on downloading. But after some time nothing happens and no error or exception is shown. The same works fine in the device of OS version below Oreo.
Below is the code used.
private void startDownload(String downloadPath, String destinationPath) {
Uri uri = Uri.parse(downloadPath); // Path where you want to download file.
File destinationFile = new File(Environment.getExternalStorageDirectory() + "/folder_name");
destinationFile.mkdir();
destinationFile = new File(destinationFile.getAbsolutePath(), "file_name.pdf");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); // Tell on which network you want to download file.
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // This will show notification on top when downloading the file.
request.setTitle("Downloading a file"); // Title for notification.
request.setVisibleInDownloadsUi(true);
request.setDestinationUri(destinationFile); // Storage directory path
((DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); // This will start downloading
}
Have enabled required permissions and tried setting Network type as well but no luck. Please help me with this.
Thanks in advance.
I am developing an app that grabs a new json file every time is starts. But I've found that even though I delete the file and download the new one, the phone somehow still ends up with the old file.
Delete code:
if (test.exists()) {
test.delete();
Log.i(TAG, "Deleting File");
} else {
Log.i(TAG, "File does not exist");
}
Download Code:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(path));
request.setTitle("stations.json");
request.setDescription("File is being downloaded.....");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setDestinationInExternalFilesDir(getApplicationContext(), null, "stations.json");
request.setVisibleInDownloadsUi(false);
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
jsonRef = manager.enqueue(request);
I've deleted the file myself from the storage. It still grabs the old file. I've checked in a browser if the server version is updated. It was. I'm at a loss as to what may be happening. If the Stack community has any suggestions, it would be greatly appreciated. Thanks so much!
It appears that my phone was somehow caching the file. It would see that I was downloading the same file and would instead just give it to me. By giving it a new url, it tricked the app into thinking it was a new file.
In the end, what worked was to add:
?v=" + Math.random()
to the end of the file.
For Example:
String path = example.com/stations.json?v=" + Math.random();
Then download with:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(path));
I have a DownloadManager to download video so that I can use player to play. But when I start to download, it has generated the unfinished .mp4 file, so when I tried to play before finish downloading, it will show error, is that possible change its extension before finish downloading?
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE).setDescription("caching video").setDestinationInExternalPublicDir("/Android/data/", id + ".mp4");
long id = manager.enqueue(request);
You can solve your problem in a little different way
Instead of change its extension before finish downloading you need to start the download with the changed extension like this:
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE).setDescription("caching video").setDestinationInExternalPublicDir("/Android/data/", id + ".mp4.tmp");
and once download is finished, rename file to "/Android/data/", id + ".mp4"
I am generating an excel sheet from search results in my app when user selects 'generate excel' option provided in my app overflow actionbar. I could save that file in the location "file:///storage/emulated/0/Download/tempfile.xls" without any problem but I would like to provide the generated excel sheet in the download menu via DownloadManager like this -
String filepath = "file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/tempfile.xls";
Uri uriDownloadFile = Uri.parse(filepath );
DownloadManager.Request request = new DownloadManager.Request(uriDownloadFile);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "tempfile.xls");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // to notify when download is complete
request.allowScanningByMediaScanner();// if you want to be available from media players
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
manager.enqueue(request);
I tried to change the the local filepath to a uri using Uri.parse (second line of code above). However, I ended with the following error -
java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: file:///storage/emulated/0/Download/tempfile.xls
This question already has answers here:
Set custom folder Android Download Manager
(4 answers)
Closed 5 months ago.
I am facing tow issues in my app.
1- I am using Android DownloadManager to download online files but it downloads all files to system directories say Download directory by using
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, "fileName");
Now I want to download the file to my own directory say "New Folder". How is it possible to implement. Please help me in this respect, I would be very thankful.
2- How can I check that whether SD card is available or not?
DownloadManager
1)
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"));
2)
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
// yes download code
}
else
{
// No sdcard
}