3

I am trying to use a ContentProvider to download a single, huge zip file to the internal storage using the DownloadManager as mentioned here (see option 2):

Create a ContentProvider which DownloadManager can write to and give it a URI that corresponds to this ContentProvider.

Can someone provide me with an example on how to achieve that? Is this what I'm trying to achieve?

Or is it a totally bad idea to do it this way?

Community
  • 1
  • 1
felixd
  • 383
  • 2
  • 7
  • 20

1 Answers1

4

The answer you posted mentions a couple of scenarios:

  1. Where you want this file to be downloaded, if you use Download Manager certainly it will be treated as public download with other app can have access it.
  2. If you want it to be application context specific internal storage then, download it via any possible method, as the poster suggested use ContentProvider.

Now to add further, no matter what method you choose, code for downloading data would remain same. Perhaps I would suggest a more suitable solution, as ContentProvider are generally related for dealing with databases so drop that, instead

  1. Create a service, this will run in background
  2. Through this service spawn a thread for downloading data, this is very important no matter which method you use but if you try to hook any long network operation over main thread, android will close you app stating Network on main thread or ANR
  3. Upon completion of downloading you can stop your service, and do whatever you want from there.

Brilliant code for downloading file below along with usage of power manger etc. can be found here:

Download a file with Android, and showing the progress in a ProgressDialog

Community
  • 1
  • 1
Techfist
  • 4,314
  • 6
  • 22
  • 32
  • But how can I handle the download across multiple system reboots, as the DownloadManager does? – felixd Dec 18 '13 at 11:05
  • you can employ various mechanism, like have a flag in preference saying download incomplete initially, if download is completed successfully mark it download complete, now abruptly if app closes or download failes you can always check through this preference about status of your download and then can decide wthr to start downloading service again or not. – Techfist Dec 18 '13 at 11:29
  • OK, but it's a file of approx. 200 MB. And if the download didn't complete, is there a way how I can resume it rather than restart it? That is the core question. And I would need to start the service after the system reboot but that should be possible. (See [this](http://stackoverflow.com/a/5439320/2210921)) – felixd Dec 18 '13 at 11:46
  • Restarting a service is a piece cake, in both cases when device reboots or in case service stops abruptly link which you posted above has details for making a service persistable. this http://stackoverflow.com/questions/6237079/resume-http-file-download-in-java will help you in implementing resume logic. – Techfist Dec 18 '13 at 12:03
  • OK. Thank you very much! Now I am able to fix this problem. – felixd Dec 18 '13 at 12:11