获取以MB为单位的视频大小Android
问题描述:
我想从android应用中的Url获取兆字节的视频大小。我有一个播放器在应用内播放视频。我想在播放器旁边显示当前的视频大小。所以一个例子是我23MB。如果视频是23毫克,比我在23MB视频旁边的文字要多。我尝试了解所有的android mp.get函数,但无法找到Iam正在寻找的东西。请帮忙。也许我错过了Android中的一个功能。或者mayber有一种方法可以帮助实现这一点。谢谢。获取以MB为单位的视频大小Android
答
HTTP content-length
标头的值将提供正在下载的文件的大小。
查看URLConnection.getHeaderField(String key)
或HttpMessage.getFirstHeader(String name)
,具体取决于您的服务器访问代码。
答
Try this will work in case the http server is giving the file size
URL myUrl = new URL("http://your_url.com/file.mp3");
URLConnection urlConnection = myUrl.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
file_size = file_size /1024;
Or second Version try this.
URL myUrl = new URL("http://your_url.com/file.mp3");
myConnection = myUrl.openConnection();
List headersize = myConnection.getHeaderFields().get("content-Lenght");
+0
Oww。我刚刚开始工作并发布了适用于我的代码。不管怎么说,还是要谢谢你。 – 2012-08-07 06:48:52
我会研究这个。谢谢 – 2012-08-07 06:01:44
好吧,我得到它的工作。但花了一个小时研究。 这是可用的代码。 int lenghtOfFile = 0; \t \t \t \t尝试{ URL url = new URL(uri.toString()); URLConnection conexion = url.openConnection(); conexion.connect(); lenghtOfFile = conexion.getContentLength(); } \t \t catch(Exception e){ e.printStackTrace(); } \t \t \t TextView p =(TextView)findViewById(R.id.videoBuffer); p =(TextView)findViewById(R.id.videoBuffer); \t \t p.setText(“”+ lenghtOfFile); – 2012-08-07 06:47:45