DownloadManager enqueue queue file and keeps on downloading in Oreo and above - android

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.

Related

Android doesn't grab new json file

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

Android Download Manager Store/Show downloaded files in two directories

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.

Can only download HTTP/HTTPS URIs error

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

Customizing a download notification

Based on another SO answer, I am using the following lines of code to download media files:
DownloadManager.Request r = new DownloadManager.Request(Uri.parse(uri));
// This put the download in the same Download dir the browser uses
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_PODCASTS, fileName);
// When downloading music and videos they will be listed in the player
// (Seems to be available since Honeycomb only)
r.allowScanningByMediaScanner();
// Notify user when download is completed
// (Seems to be available since Honeycomb only)
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// Start download
DownloadManager dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(r);
Now, that is all good except that I have a few questions:
The person who answered said that the notifications can be customized. How can that be done?
If I want to add a "cancel" button to cancel the download and delete the file, how do I do it? What do I need to implement to get this behavior? :)
// Changing the Notification Title, Description, and its Visibility
DownloadManager mgr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("///.mp3");
DownloadManager.Request req = DownloadManager.Request(uri);
req.setTitle("Download");
req.setDescription("Kick Ass Description");
req.setVisibility(Request.VISIBILITY_VISIBLE_COMPLETION_ONLY);
Pretty self-explanatory.

How to set an icon for the DownloadManager Request?

I'm trying to set an icon for the DownloadManager request that I created, but I can't manage to do that.
I read about creating a BroadcastReceiver, but I was wondering if it's possible to do that without it.
This is my code for the DownloadManager Request:
final String filename = getIntent().getExtras().getString("id") + ".apk";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(filename);
request.setDescription("Downloading...");
request.setMimeType("application/vnd.android.package-archive");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir("/Library", filename);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Toast.makeText(LinkActivity.this, "Download has started.", Toast.LENGTH_LONG).show();
Here's an example of what it looks like now:
My target is to change the image of the notification ImageView on the left.
Thank you very much.
You cannot override this icon. And, if I remember correctly, it would make no much sense to be able to, as download manager can have more than one downloading running and it will still take one notification slot to indicate that...

Categories

Resources