The best way to download a big file in android? - android

I need to download a big file on my app (almost 2gb) and i was wondering what is the best way to do that and some libs to help me.
First I looked the Android Asynchronous Http Library but I didn't find examples showing how to publish the progress or start, pause download. Then I don't know if should I use this lib or just use a standart http commons.
Other problem is, should I use a service to download my file??
Could you guys give a hint, how to do that?

I've see you got a bunch of answer and none of them really answers it from a holistic point of view.
To download a 2k or 2gb file the actual code is the same using some type of connection handler and some type of input stream and output stream and usually wrapping the input stream with a buffered input stream. For that you can find infinite amount of java examples all around the web.
The trick here considering the Android platform is that because it's a lengthy operation, you shouldn't be doing it inside the activity life cycle at all.
Said that you have two options:
as you suggested you can create a Service, I would suggest you to use the IntentService
as it seems to be a perfect fit for this case. The IntentService automatically spans a new thread, through the intent you pass the URL as a String and let the service download using the streams, etc.
another method that probably work well, but I've never personally used, is to use the DownloadManager that is available since Gingerbread. It shouldn't be difficult to call getSystemService(Context.DOWNLOAD_SERVICE) and call dwManag.enqueue(dwRequest);
hope it helps.

If you're not targeting pre-Gingerbread devices, I would use DownloadManager as suggested as the third option in the answer you linked to. It takes care of downloading the file, displays the progress in the notification bar so that the user can see what's going on even after your app has gone into the background and you don't have to worry so much about what happens when the user goes into another app and android decides to kill your app to free memory. Also, it works with features like "only download files over wifi" that at least some android builds have.

I suggest you to use adm download manager. Downloads never fail even if there is no network and the speed is also best.

Related

Any way to resume download from last break point Android?

I am making a download manager for my app. It works fine, but as soon as network fluctuates, I have to press retry button and it starts downloading from the beginning again. Any way to resume it from last break point?
Thanks,
Rahul
As the documentation (http://developer.android.com/training/volley/index.html) says
Volley is not suitable for large download or streaming operations, since Volley holds all responses in memory during parsing. For large download operations, consider using an alternative like DownloadManager.
However you could use the volley calls to download your big file in chunks, then when you have server instability you could just request the chunks you don't have. Note this is generally a bad idea as you're basically recreating tcp and you shouldn't do that.
create a temporary directory and store the fragment of the files that is being downloaded. after downloading concatenate the files.

Using robospice for long running http requests

Is there any example of using robospice library for downloading large files? I've read there is BigBinaryRequest for it but what if there will be connectivity lost/ device reboot duing file download? After next execute call download will resume/restart or request success listener will fire with reference to damaged (not completly downloaded) file?
Maybe someone have experience of using robospice for such requests.
P.S. I know that there is native DownloadManager in Android, but I think using robospice is easier. Maybe I'm wrong.
#rciovati got it right, your download, if interrupted will be lost as RS won't be able to load the result from the cache. Or even worse, you could have receive an uncomplete InputStream from the cache. In that case you should remove the cache content by yourself (using the spicemanager's method to achieve it is pretty easy).
If you download twice a large input stream using the same cache key, there is no protection against that in RS. Your cache will get corrupted.
This answer may give you the feeling that BigBinaryRequest is poorly designed and not working, but according to my own experience, it works fine in all cases I met up to now.

Android Best way to use AsyncTask

I've been looking at this guide:
http://developer.android.com/training/basics/network-ops/connecting.html
And was wondering what would be the best way to download multiple files. First I need to download a text file from a url to determine which files to download.
Should I have 2 separate ASyncTasks, one to download the file and then the other to download the remaining files? Otherwise my code which depends on the first file crashes since the Async task does not complete in time.
Also for the progress dialogue should I make a new one for each file or try to update the previous one?
Thanks
Orginally I was creating a new AsyncTask for each file to download.
In general, if you want the files to remain on the devices, and you're downloading multiple files based on the results of downloading one file, then you should
Use an IntentService to download and store/parse the first file.
Use an IntentService to read the results of the first download and then download the remaining files. If you need to, you can use a progress bar notification in the notification area. Meanwhile, the user can continue working in the app or even switch to another app and the download will continue.
An IntentService is immune to Activity lifecycle changes that might kill an AsyncTask.
Any time you download data, persist it somewhere. You can always check to see if the data is outdated. On the other hand, if there's no connectivity, users have the last "good" data.
To learn more about IntentService, see Running in a Background Service. The content provider in the sample app illustrates downloading "metadata" for other files. The sample also demonstrates how to check for connectivity before downloading.
There is no perfect answer to cover every situation possible.
If you are happy with one quick running AsyncTask, don't change anything.
If you are using API 9+, you could switch to the DownloadManager class and let it workout the particulars.
If you need references, Download a file with Android, and showing the progress in a ProgressDialog, provides examples for multiple ways to download a file with an active ProgressBar.

Downloading multiple files simultaneously in Android applications

I'm writing an application for Android which let users browse a list of files and download them.
For every download, I created a thread and I download the file with an HttpURLConnection instance (by reading from the connection in a while loop).
This method works fine with one active download. But when user starts more than one, download performance degrades dramatically. Most of the time, these parallel downloads consume all the bandwidth and the users is unable to browse files (which uses another HttpUrlConnection to load the files list).
Any suggestions on refining the download system?
Thanks.
P.S.: The method that popular browsers such as Google Chrome and Firefox do seems good. Anyone knows how they work?
Alas, i don't know of a way to throttle certain connections. However, a practical approach would be to implement a queue of downloads to control the number of simultaneous downloads. In your case, you would probably want to only let 1 thing download at a time. This can be implemented a few different ways.
Here's a way to do it with Handlers and a Looper: http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/
Edit 1:
See mice's comment. It may be smarter to have a max of 2 threads downloading at a time.
You might want to check out the DownloadManager class in the android SDK.. Its only available above or equal api level 2.3 though.
http://developer.android.com/reference/android/app/DownloadManager.html
Some tutorials you might want to see..
http://jaxenter.com/downloading-files-in-android.1-35572.html
http://www.vogella.de/blog/2011/06/14/android-downloadmanager-example/

How do you force android to download a podcast instead of playing it?

I'm trying to find a way to bypass the selection pop-up for podcasts and just tell android to download the file.
Currently when you open a link to a podcast a little menu pops up and has two buttons. One says download, and one says listen. Instead I want "Download" or "Cancel".
I have looked into this and I found the DownloadManager class, but sadly (I think) it is only for 2.3, and the app is being developed for 2.2.
Does anyone know how android did file downloading before this? Is there a specific intent I can pass the link to?
Thanks!
Well, without code I can only guess.
It sounds as if you have a Web View or Browser that is interpreting the url being opened based on part of the url scheme. This is causing the operating system to try to resolve activities that are available for this url. You're seeing two activities (download or listen) which are supported by activities available on the phone. In this situation you will need to overload the loading of urls using this tutorial,
Of course, without some crystal ball I can't be certain of this based on what you've said. If this is the case and you want to know how to download it. You would need to build a downloader of your own, which is possible using Java Streams, Java NIO or some of the Apache HTTP Client libraries. Based on the size of your files, it may make sense to use Buffered Java Streams with the Apache Http Client objects.
You can find an example of how to do this here.

Categories

Resources