English 中文(简体)
URLConnection.getContentLength() Return -1
原标题:URLConnection.getContentLength() returns -1
  • 时间:2012-05-03 21:52:24
  •  标签:
  • android

我有一个URL,在我进入浏览器时,它完全打开了图像。 但是,在我尝试以下法典时,我得到的是:

URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// determine the image size and allocate a buffer
int fileSize = connection.getContentLength();

请指导我这样做的原因是什么?

最佳回答

如果服务器使用Chunked TransferEncoding,你将无法预先确定大小。 答复是分门别类的,你只得分配一个缓冲,储存图像,直至上游完成。 请注意,只有你能够保证图像足够小,足以保存记忆,你才能这样做。 如果图像可能大,简化对闪电储存的反应是完全合理的选择。

In-memory solution:

private static final int READ_SIZE = 16384;

byte[] imageBuf;
if (-1 == contentLength) {
    byte[] buf = new byte[READ_SIZE];
    int bufferLeft = buf.length;
    int offset = 0;
    int result = 0;
    outer: do {
        while (bufferLeft > 0) {
            result = is.read(buf, offset, bufferLeft);
            if (result < 0) {
                // we re done
                break outer;
            }
            offset += result;
            bufferLeft -= result;
         }
         // resize
         bufferLeft = READ_SIZE;
         int newSize = buf.length + READ_SIZE;
         byte[] newBuf = new byte[newSize];
         System.arraycopy(buf, 0, newBuf, 0, buf.length);
         buf = newBuf;
     } while (true);
     imageBuf = new byte[offset];
     System.arraycopy(buf, 0, imageBuf, 0, offset);
 } else { // download using the simple method

从理论上讲,如果Http客户将自己列为HTTP 1.0,大多数服务器将转而采用非主流模式,但我并不认为这是URLConnection的一种可能性。

问题回答

我在此很晚,但这可能有助于一个人。 我也面临同样的问题,即总是获得-1价值,当时我一直在试图获得内容长度。

以前使用以下方法获得内容长度。

long totalByte=connection.getContentLength();

以下是我的问题:

long totalByte=connection.getHeaderFieldLong("Content-Length",-1);




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签