title: URLConnection getContentLength() is returning a negative value
body: <p>Here is my code:</p>
  
  <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 documentation and the length is negative because of 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. I would like to point out that this is the fourth method that I am trying to download images. The other three are mentioned <a href="https://stackoverflow.com/questions/5360618/android-big-error-while-downloading-images">here</a>.</p>
  
  <p><strong>Edit:</strong> </p>
  
  <p>As requested, here is the full method I am using.</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>