Download an HTTP file to SDcard with progress notification

10 votes · 14 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

14 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.

how do i define the textview element for mProgressText

Reply · April 14, 2012, 10:35 a.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.

Do you have your permissions set correctly?

Reply · April 1, 2014, 8:37 a.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.

It would be really helpful if you can provide a snippet to create expansion files? For example, I want to upload mp3 files in zip folders as expansion files and force users to download to SD card before installing apk from android market. Then read mp3 from sd card to actual application.

Reply · April 19, 2012, 1:10 p.m.

Do u have another code samples with the function of upload an audio file to .net web (localhost) ? Share please, thanks a lot...

Reply · May 14, 2012, 2:58 a.m.

Hi, it is better if you place fileOutput.close() and inputStream.close() in a finally block, that will make sure you dont have any memory leaks, even if exceptions are thrown,

Reply · April 1, 2014, 8:36 a.m.