2

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') ?

Community
  • 1
  • 1
realtebo
  • 23,922
  • 37
  • 112
  • 189

1 Answers1

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

obataku
  • 29,212
  • 3
  • 44
  • 57
  • gives android.os.NetworkOnMainThreadException – Sagar Pujari Jul 21 '16 at 12:53
  • @SagarPujari that has everything to do with your code and not the snippet above; you are attempting to do blocking network I/O in the context of the application's main UI thread. consider using an [https://developer.android.com/reference/android/os/AsyncTask.html] to offload the work to run in a background thread – obataku Jul 21 '16 at 22:51