Stormtap Studios wrote:
> 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.
Try switching to HttpClient. You can use this as a basis:
package org.apache.http.examples.client;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* This example demonstrates the use of the {...@link ResponseHandler} to
simplify
* the process of processing the HTTP response and releasing associated
resources.
*/
public class ClientWithResponseHandler {
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.google.com/");
System.out.println("executing request " + httpget.getURI());
// Create a response handler
ResponseHandler<String> responseHandler = new
BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
System.out.println(responseBody);
System.out.println("----------------------------------------");
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
You'd need to create a BinaryResponseHandler class that gave you a
byte[] of data. I did this once for a consulting client -- it was an
extremely short class IIRC. Or, skip the ResponseHandler pattern, call
httpclient.execute() and use the resulting HttpResponse object.
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
_Android Programming Tutorials_ Version 2.0 Available!
--
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
To unsubscribe from this group, send email to
android-beginners+unsubscribegooglegroups.com or reply to this email with the
words "REMOVE ME" as the subject.