getContentLength()返回-1在某些设备上,而不是其他人 [英] getContentLength() returning -1 on some devices and not others

查看:175
本文介绍了getContentLength()返回-1在某些设备上,而不是其他人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以前我下载获取文件的大小。我用 conn.getContentLength(); 做到这一点,它工作正常在我的家用电脑的Andr​​oid 2.1模拟器

I'm trying to obtain the size of a file before I download it. I use conn.getContentLength(); to do this and it works fine on my home computers Android 2.1 Emulator.

但它不能正常工作,一旦我从我的手机(无论是WiFi或3G)上运行我的应用程序,它也当我从我的作品中运行它不工作,笔记本电脑的Andr​​oid 2.1模拟器。

It however doesn't work once I run my app from my phone (either WiFi or 3G) and it also doesn't work when I run it from my work laptops Android 2.1 Emulator.

有谁知道解决方法吗?有另一种方式,我可以获取该文件的大小可能不使用的HttpURLConnection

Does anyone know a workaround for this? Is there another way I can obtain the size of the file maybe without using HttpURLConnection.

推荐答案

这个信息不会始终可用。通常你会知道你正在下载的文件的长度。根据不同的网络服务器,协议,连接,并下载的方法,该信息可能不总是可用的。

This information will not always be available. Usually you will know the length of the file you are downloading. Depending on the webserver, the protocol, the connection, and the method of downloading, this information may not always be available.

您一定要修改应用程序,以便它可以处理这种情况。我想你会使用不同的连接方式将提供不同的结果与此发现,不同的设备。

You should definitely modify your application so that it can handle this situation. I think you will find that different devices using different connection methods will offer different results with this.

这篇关于getContentLength()返回-1在某些设备上,而不是其他人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆