URLConnection getContentLength() 返回负值

2024-03-16

这是我的代码:

url = paths[0];
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int length = connection.getContentLength(); // i get negetive length
InputStream is = (InputStream) url.getContent();
byte[] imageData = new byte[length]; 
int buffersize = (int) Math.ceil(length / (double) 100);
int downloaded = 0;
int read;
while (downloaded < length) {
    if (length < buffersize) {
        read = is.read(imageData, downloaded, length);
    } else if ((length - downloaded) <= buffersize) {
        read = is.read(imageData, downloaded, length - downloaded);
    } else {
        read = is.read(imageData, downloaded, buffersize);
    }
    downloaded += read;
    publishProgress((downloaded * 100) / length);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,
        length);
if (bitmap != null) {
    Log.i(TAG, "Bitmap created");
} else {
    Log.i(TAG, "Bitmap not created");
}
is.close();
return bitmap;

我在 Java 文档中查看了这一点,长度为负数,原因如下:

“内容的字节数,如果未知则返回负数。如果内容 >length 已知但超过 Long.MAX_VALUE,则返回负数。”

这可能是什么原因?我正在尝试下载图像。我想指出的是,这是我尝试下载图像的第四种方法。其他三个都提到了here https://stackoverflow.com/questions/5360618/android-big-error-while-downloading-images.

Edit:

根据要求,这是我正在使用的完整方法。

protected Bitmap getImage(String imgurl) {

    try {
        URL url = new URL(imgurl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        int length = connection.getContentLength();
        InputStream is = (InputStream) url.getContent();
        byte[] imageData = new byte[length];
        int buffersize = (int) Math.ceil(length / (double) 100);
        int downloaded = 0;
        int read;
        while (downloaded < length) {
            if (length < buffersize) {
                read = is.read(imageData, downloaded, length);
            } else if ((length - downloaded) <= buffersize) {
                read = is.read(imageData, downloaded, length
                        - downloaded);
            } else {
                read = is.read(imageData, downloaded, buffersize);
            }
            downloaded += read;
        //  publishProgress((downloaded * 100) / length);
        }
        Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,length);
        if (bitmap != null) {
             System.out.println("Bitmap created");
        } else {
            System.out.println("Bitmap not created");
        }
        is.close();
        return bitmap;
    } catch (MalformedURLException e) {
        System.out.println(e);
    } catch (IOException e) {
        System.out.println(e);
    } catch (Exception e) {
        System.out.println(e);
    }
    return null;
}

默认情况下,HttpURLConnection 的此实现请求服务器使用 gzip 压缩。
由于 getContentLength() 返回传输的字节数,因此您无法使用该方法来预测可以从 getInputStream() 读取多少字节。
相反,读取该流直到耗尽:当 read() 返回 -1 时。
可以通过在请求标头中设置可接受的编码来禁用 Gzip 压缩:

 urlConnection.setRequestProperty("Accept-Encoding", "identity");

所以试试这个:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Encoding", "identity"); // <--- Add this line
int length = connection.getContentLength(); // i get negetive length

来源(性能段落):http://developer.android.com/reference/java/net/HttpURLConnection.html http://developer.android.com/reference/java/net/HttpURLConnection.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

URLConnection getContentLength() 返回负值 的相关文章

随机推荐