Download files


this will allow you to download a file from the internet, this has been copy from part of my app called “Root Toolbox Pro”.

try {
URL input = new URL(“http://dl.dropbox.com/u/22618820/open%20source/Toolbox/in%20app%20downloads/Gapps-2.3-Universal.zip”);
URLConnection conn = input.openConnection();
int contentLength = conn.getContentLength();

DataInputStream stream = new DataInputStream(input.openStream());

byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();

DataOutputStream output = new DataOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory() + “/Gapps-Update.zip”));
output.write(buffer);
output.flush();
output.close();
} catch(FileNotFoundException e) {
return;
} catch (IOException e) {
return;
}

also these permission will need to be added to the manafest to allow your application to access the internet and save files to te sdcard.

<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”></uses-permission>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: