Tuesday, February 7, 2012

Android Tutorial - Download File


This is how you can easily download a file from drop box or any other place you have files stored. Just plug in the website URL and the file you want download. 
try {
URL input = new URL("WHAT_URL_YOU_USE_TO_DOWNLOAD_FILE");
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() + "/WHATEVER_FILE"));
output.write(buffer);
output.flush();
output.close();
} catch(FileNotFoundException e) {
return;
} catch (IOException e) {
return;
}
These permissions are needed for it work to properly.
<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”></uses-permission>
 Download Our Blog for Android!

No comments:

Post a Comment