Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know the size of a file before downloading it?

I have to download a file and I'm using this code, which is basically an AsyncTask that is meant to update a progress bar. But, since I don't know what's the file size I've been having to use the spinner progress bar. So, how can I get the file size before start downloading it so that I can use a normal progress bar?

like image 614
Cristian Avatar asked Jun 06 '10 04:06

Cristian


People also ask

How do you know the size of a file?

Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.

How do I see download size in Chrome?

If you visit the Chrome download Web page and click "Accept and Install," a dialog window opens that offers to save the file. It also shows the file's size. As of the date of publication, the size of the 23.0.

Why do you need to know the file size when you create download or save any files into your computer?

Every file on a computer uses a certain amount of resources when sent over the internet or stored. Keeping mind of your kilobytes (kB) and megabytes (MB) can prevent problems and produce a smoother online experience.


1 Answers

you can get a header called Content-Length form the HTTP Response object that you get, this will give you the length of the file. you should note though, that some servers don't return that information, and the only way to know the actual size is to read everything from the response.

Example:

URL url = new URL("http://server.com/file.mp3"); URLConnection urlConnection = url.openConnection(); urlConnection.connect(); int file_size = urlConnection.getContentLength(); 
like image 94
reflog Avatar answered Oct 07 '22 17:10

reflog