url connection returning negative value
<pre><code>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 &lt; length) {
                if (length &lt; buffersize) {
                    read = is.read(imageData, downloaded, length);
                } else if ((length - downloaded) &lt;= 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;
</code></pre>

<p>I looked at this in java doc, the length is negative because the following reason:</p>

<blockquote>
  <p>"the number of bytes of the content, or a negative number if unknown. If the content >length is known but exceeds Long.MAX_VALUE, a negative number is returned."  </p>
</blockquote>

<p>What might be the reason for this. I am trying to download an image</p>

<p>Also , This is the fourth method that i am trying to download images along with the other  three mentioned in this question.</p>

<p><a href="http://stackoverflow.com/questions/5360618/android-big-error-while-downloading-images">Android : Big error while downloading images.</a></p>

<p>Not got the answer still.</p>

<p>Edit: The complete code of the method that i am using is as below</p>

<pre><code>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 &lt; length) {
        if (length &lt; buffersize) {
            read = is.read(imageData, downloaded, length);
        } else if ((length - downloaded) &lt;= 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;
</code></pre>

<p>}</p>
