Downloading multiple files simultaneously in Android applications - android

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/

Related

Reducing apk size for a difference game (to allow incremental downloads)

I am working on a difference game (for android).
The apk size becomes large when I add too many levels(say 100+). I have optimized all images, etc. However the size become more than 10 Mb.
Is there any way to allow users to incrementally download 10 image files (png) at each time.
Something like only first 10 images with app install and then allowing incremental 10 images with each button click from within the app.
If I try to do it by downloading from a web server, what will I need to implement (is it async task or can I do it with something simple). In such cases, is http allowed or I need https.
I'd personally rather download that 10MB once and for all instead of downloading those additional images every time I play the game; it could be expensive to download all that data. You could of course cache them, so I don't have to download them again. And you could foreward-download an entire level of images at a time (level2.zip). But what if I suddenly beat my own high score and proceed to a new level ... and I don't have a very good internet connection at that particular moment? If you're ruining my gaming experience, I'll delete your app and give it a bad review immediately -- and so will everyone else =)
I'd definitely recommended adding all data and not depend on the user having a fast internet connection AND is willing to pay for the download.
If you app grows too big, you should look into expansion files, but I havne't used those myself so I can't tell much more than the link:
https://developer.android.com/google/play/expansion-files.html

Site parsing and opening with slow internet connection

I am making a network app that works with a few news sites. I have an option to view and download articles (for reading in future). There is no fast mobile internet in my country (mostly 2G with the speed up to 15 KB/sec) and my app doesn't work really well with the slow connect though it's perfect with fast connection. As I have seen from logs it can't establish the connection. When I try to open the article it doesn't load at all or goes into infinite loading. When I try to save the article it simply saves the blank file. How can I track these mistakes and catch them? And what are the correct ways of handling it?
I suppose that my code is irrelevant as it works correctly with Wi-fi. I think I simply need to add lines, not to edit them
ok, you need to manage the information very good, you need define the package size to download, you need a webservices that allow to you to manage the information one by one, for example you need design packages no more than 500kb size to download, when this package is downloaded you can show something in your mobile and continue downloading the next package in background.
500kb is only an example you need to test what is the maximum data size to download and create packages like that in your web services

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.

How to boost the speed of downloading files in the background in Android?

I am developing an app (for both Android and iPhone). Actually, in my app I have to download lot of videos in background (using a background service, IntentService) as well as I have a screen showing the progress of downloads (using an Activity which has UI for showing download progress). But I don't know why the download speed is too less as compared to download speed for the same app in iPhone.
Also, after each download of video, I am marking that video as downloaded in the database. And this happens for all videos.
Is the database calls a problem for slow downloads of files in android?
Since the same functionality does not affect the download speed of files in iPhone.
You could try to implement some sort of download accelerator which downloads a single file by splitting it in segments and using several simultaneous connections to download these segments from a the server. First you should check if the server Accepts byte-ranges and then specify byte-ranges to download in a multi-threaded way. At last you just merge back the segments into a complete file.
Look into these documents for more info:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.5
Page 128:
http://www.ietf.org/rfc/rfc2068.txt
Also look up Java API URLConnection.setRequestProperty
However, I think the slow download is mostly because of your network bandwidth and server's upload speed.
Increase the Byte size of your inputstream
Update:
Take a look here

The best way to download a big file in 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.

Categories

Resources