Download an HTTP file to SDcard with progress notification

8 votes · 9 comments

I'm not the original author of this code. It was found here: http://chrischurchwell.com/.../android-download-file-from-internet-to-sd-card-with-progress-report/. Unfortunately, the website seems down as from 25/05/2010.

If you're the author and would like me to do something about this (remove, edit, whatever) please contact me.

I thought this might be useful. It's a pretty frequent task and there seem to be no snippets available on the Internet.

raw ·
copy
· download
try { //set the download URL, a url that points to a file on the internet //this is the file to be downloaded URL url = new URL("http://somewhere.com/some/webhosted/file"); //create the new connection HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //set up some things on the connection urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); //and connect! urlConnection.connect(); //set the path where we want to save the file //in this case, going to save it on the root directory of the //sd card. File SDCardRoot = Environment.getExternalStorageDirectory(); //create a new file, specifying the path, and the filename //which we want to save the file as. File file = new File(SDCardRoot,"somefile.ext"); //this will be used to write the downloaded data into the file we created FileOutputStream fileOutput = new FileOutputStream(file); //this will be used in reading the data from the internet InputStream inputStream = urlConnection.getInputStream(); //this is the total size of the file int totalSize = urlConnection.getContentLength(); //variable to store total downloaded bytes int downloadedSize = 0; //create a buffer... byte[] buffer = new byte[1024]; int bufferLength = 0; //used to store a temporary size of the buffer //now, read through the input buffer and write the contents to the file while ( (bufferLength = inputStream.read(buffer)) > 0 ) { //add the data in the buffer to the file in the file output stream (the file on the sd card fileOutput.write(buffer, 0, bufferLength); //add up the size so we know how much is downloaded downloadedSize += bufferLength; //this is where you would do something to report the prgress, like this maybe updateProgress(downloadedSize, totalSize); } //close the output stream when done fileOutput.close(); //catch some possible errors... } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
Add a comment

9 Comments

Thanks for the snippet!

Reply · May 25, 2011, 8:45 p.m.

is that rewrite the file or replace the file?

u put updateProgrss method in that codes, where is updateProgress function, because I get some error when I copied it.

Reply · June 21, 2011, 6:47 a.m.

is that or internat or external data storage?

Reply · June 21, 2011, 8:06 a.m.

You have to write the updateProgress method yourself. It is not provided by the code. I used something like this: public void updateProgress(int currentSize, int totalSize){ mProgressText.setText(Long.toString((currentSize/totalSize)*100)+"%"); } (where mProgressText is a TextView element). The code will write the file to your SD Card's root folder (external storage). Remember to set the permission to do so in your manifest file with

Reply · June 23, 2011, 12:13 p.m.

with the permission : android.permission.WRITE_EXTERNAL_STORAGE

Reply · June 23, 2011, 12:15 p.m.

when i look into my ddms, the file has been wrote into my sd card, but it don't have a size, just a name of the file. when i tried to push the file to my computer, it can't be opened... can you tell me the solution? thanks before

Reply · July 27, 2011, 12:55 p.m.

Thanks a whole lot for this snippet.............Just saved my life :-D

Reply · Sept. 5, 2011, 12:22 p.m.

Just good............

Reply · Jan. 2, 2012, 1:38 a.m.

Provide online to buy maternity dress, source: www.fashionmaternitydress.com www.bangaccessory.com

Reply · Jan. 11, 2012, 7:51 a.m.