5

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?

Community
  • 1
  • 1
Victor Laerte
  • 6,446
  • 13
  • 53
  • 102

3 Answers3

13

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.

Budius
  • 39,391
  • 16
  • 102
  • 144
1

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.

thejh
  • 44,854
  • 16
  • 96
  • 107
0

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