Android:下载前如何获取文件大小?

Android:下载前如何获取文件大小?

问题描述:


可能重复:

在下载文件之前如何知道文件的大小?

我必须在我的应用程序中下载一些pdf。

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

如何获取具有新Url的文件大小(' http://mysite.net//mypdf.pdf ')?

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

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

尝试使用 URLConnection.getContentLength 如上。如果服务器没有指定长度,那么您不能知道。

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