Download manager in android? - android

Hi have simple app in which data has been uploaded from the web server I am using .net web services. I need to replace the document on same location, so that duplicate document not place on same folder.
I am using:
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS + "/Downloads",
name );
is it right approach? can I delete document from physical place and then replace it with new one. if yes How this thing is possible.
this is my download manager and I am trying to delete name document and then trying to replace with new one. Please suggest. thanks
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(name);
// 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(
Environment.DIRECTORY_DOWNLOADS + "/Downloads", name);
DownloadManager manager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

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

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

Download manager does not recognise .mp3 files

I am using Download Manager
and i want to download some audio files using my own webview.
I use the following code:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url2));
request.setDescription("Downloading");
request.setTitle("File :");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "audio.mp3");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Even if the the File Name is audio.mp3 download manager does not recongise the file type.
ScreenShot explains the main problem.
Using other apps like default browser downloaded files look like this!
The solution is simple:
Just add the following line
request.setMimeType("audio/MP3");
More info here
not work : request.setMimeType("audio/MP3");
To operate, follow the steps below.
request.setMimeType("audio/mpeg");

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...

Android Download Manager

I think i have a rather simple question.
http://www.vogella.com/blog/2011/06/14/android-downloadmanager-example/
I have been following a tutorial in the above url.
How do I change the filepath for downloads?
Thanks in Advance
You configure the DownloadManager.Request object with that sort of information. In the tutorial, that Request object is created and used in onClick().
For example:
DownloadManager.Request req=new DownloadManager.Request(uri);
req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
"test.mp4");
(above code is from this sample project)
The last line in CommonsWare's answer states the destination. He just uses the regular download-folder on the sdcard, but you could as well do this:
req.setDestinationInExternalPublicDir("/mnt/sdcard/Myfolder", "file_name.extension");
You make sure external storage access is permitted by user for your app. Include this code for the download
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url_string);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setVisibleInDownloadsUi(true); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// include this line if permission has been granted by user to external directory
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());
// or this line if not yet granted
request.setDestinationUri(Uri.parse("file://" + uri.getLastPathSegment());
Long reference = downloadManager.enqueue(request);
You can try to do following:
Visit the URL to understand about Download Manager https://github.com/quoraboy/DownloadManagerInAndroid
As far as I understand you can't pause/resume download manually.
If you cancel the download then it totally depends on your server whether they support pause/resume feature or not. If yes then, after cancelling the download, if you start the download again it may actually resume it and don't start downloading from beginning.

Categories

Resources