Download file 5-40 MB in android 2.2 from URL??

Put your problem here if it does not fit any of the other categories.

Download file 5-40 MB in android 2.2 from URL??

Postby cooldeep » Tue Feb 01, 2011 8:53 pm

I am developing an application in which i need to download a file(.zip / .txt / .jpg etc) size- 5 to 50 MB.. Application based on Android 2.2.

The user provides the URL and triggers the download but then the downloading process runs in background until complete.

streaming should be used for downloading file.
I want to know how can this be done using HTTP connections.
=>what classes can be used for this?
=>Does android 2.2 provides an API for this?

Any kind of help is appreciated....
cooldeep
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Dec 25, 2010 9:10 am

Top

Re: Download file 5-40 MB in android 2.2 from URL??

Postby jonbonazza » Thu Mar 10, 2011 2:51 am

-HttpUrlConnection
-URL
-InputStream
-FileInputStream
-OutputStream
-FileOutputStream
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Download file 5-40 MB in android 2.2 from URL??

Postby imbroken » Thu Mar 10, 2011 8:23 am

I'm using Apache HTTP Client for this.

FileOutputStream fos = null;
InputStream fis = null;
HttpGet httpRequest = null;
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is
// established.
int timeoutConnection = 2500;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 2500;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

try {
httpRequest = new HttpGet(new URI(url));
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);

int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
fos = context.openFileOutput(zipFileName,Context.MODE_PRIVATE);
fis = entity.getContent();

byte[] buffer = new byte[1024];
int length = 0;
while ((length = fis.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
fos.flush();
fos.close();
fis.close();
}
catch { } finally {//maybe better to close the streams here}
imbroken
Senior Developer
Senior Developer
 
Posts: 131
Joined: Thu Jan 06, 2011 6:57 pm
Location: Germnay

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests