在android中下载之前获取文件大小

var*_*esh 2 android

谁能告诉我如何在从 Android 中的服务器(可能是 http、ftp 或其他)下载文件之前获取文件的大小?流媒体是否有效?请帮我..

问候瓦内什

use*_*615 5

如果 http 服务器给出文件大小,请尝试此操作

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

或者第二个版本试试这个。

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

它在我的项目中有效。