varnesh
varnesh

Reputation: 21

getting the file size before downloading in android

Can anybody tell me how to get the size of a file before downloading it from server(may be http,ftp or anything) in android?.Does streaming works?. Please Help me..

Regards Varnesh

Upvotes: 2

Views: 4188

Answers (2)

LifeInstructor
LifeInstructor

Reputation: 1610

Try this will work in case the http server is giving the file size

URL myUrl = new URL("http://jamants.com/file.mp3");
URLConnection urlConnection = myUrl.openConnection(); 
urlConnection.connect();
int file_size = urlConnection.getContentLength();

Or second Version try this.

URL myUrl = new URL("http://jamants.com/file.mp3");
myConnection = myUrl.openConnection();
List headersize = myConnection.getHeaderFields().get("content-Lenght");

It Works in my project.

Upvotes: 5

Chris Thompson
Chris Thompson

Reputation: 35598

If you are using the built-in Apahce HTTP library to make a request for a file, you can get the file size by requesting the headers only. Inside the headers will be a "content-length" attribute that will indicate the number of bytes of the requested file. Requesting the headers will not request the file itself.

Upvotes: 2

Related Questions