Android: how to get file size before download? [du

2019-05-26 19:38发布

问题:

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

I must download some pdf in my app.

I need to have the filesize before start to download, to show a progress bar, or to notify the user with the standard 'downloading .. ' notification

How to get a file size having a new Url('http://mysite.net//mypdf.pdf') ?

回答1:

final URLConnection connection = url.openConnection();
final int length = connection.getContentLength();

Try using URLConnection.getContentLength as above. If the server doesn't specify a length, then you can't know.