Download a file from a URL extremely slow, am I doing it right?

by Stormtap Studios » Tue, 23 Mar 2010 04:46:49 GMT


Sponsored Links
 Hello folks,

I tried to post this on the android-developers list twice but it
doesn't seem to ever show up and I'm not sure why that is, so I'll
post here in the hopes that someone can help me, or perhaps another
beginner like myself has also run into this problem.

I have the following code to download a binary file from a URL:

private static final int BUFFER_SIZE = 1024 * 1024;

BufferedInputStream stream = new BufferedInputStream(new URL(<MY
URL>).openStream(), BUFFER_SIZE);
BufferedOutputStream fos = new BufferedOutputStream(new
FileOutputStream(outputFile));
byte buf[] = new byte[BUFFER_SIZE];
int numBytesRead;
do
{
        numBytesRead = stream.read(buf);
        if (numBytesRead > 0)
        {
                fos.write(buff, 0, numBytesRead);
        }
} while (numBytesRead > 0);
fos.flush();
fos.close();
stream.close();
buf = null;

The file that I'm downloading is 6.5MB.  On the iPhone version of my
app it takes about a minute to download, maybe less on good days.  On
the Android Dev Phone 2 I'm testing on it takes 10 - 15 minutes to
download, it's EXTREMELY slow.

I am looking for any and all advice on how to speed up the download
for large binary files.

One thing I tried was to speed up the writing to the SD card by
filling up buf, and then only doing fos.write() when the buffer was
full (so roughly 6 and a half writes instead of lots of mini writes).
This seemed to have no effect on the overall process, which confirms
IMO that the slow part is definitely reading from the URL stream.

Thanks very much.

Robert Hawkey

--



Other Threads

1. ADB rejected shell command (ls -l /)

im getting this problem very often now a days:::ADB rejected shell
command (ls -l /).
And after this nothing works in the emulator.

-- 

2. Anyone konws the ConnectivityBroadcastReceiver class?

Anyone konws the ConnectivityBroadcastReceiver class? There is
nothing clues in new SDK documents.When I ran my app,there is an
error  in background "09-05 09:53:58.889: ERROR/ActivityThread
(188):android.app.IntentReceiverLeaked: Activity
com.xxx.xxx.aa.MainActivityhasleakedIntentReceiverandroid.net.NetworkConnectivityListener
$connectivitybroadcastrecei...@4337ce70 that was originally
registeredhere. Are you missing a call to unregisterReceiver()?" and
my app still  work well. So what's the meaning of
"IntentReceiverLeaked"?How could I avoid it?Thank you very much.

-- 

3. getting error while importing com.google.googlenav.map.MapPoint; ..........

4. How to execute code before exiting application

5. Problem in setMargins() and textview

6. Help getting Hello world to go

7. my Android Market Dashboard is worrying me