Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebImageView can't display link without "Content-Length" in the response header #51

Open
benmpwong opened this issue Aug 8, 2011 · 1 comment

Comments

@benmpwong
Copy link

The image link does not necessarily contain a "Content-Length" header in their response header (at least in my test case on google finance chart like http://www.google.com/finance/chart?cht=c&q=INDEXDJX:.DJI,INDEXSP:.INX,INDEXNASDAQ:.IXIC&tlf=12h), while the ImageLoader.retrieveImageData will return null if the header does not exist.
I think the image data array could be initialized by the method doesn't involve testing the header field, here's my code (I didn't handle if the connection returning a HTTP status not equal to "OK", but that is out of the topic in this issue):

protected byte[] retrieveImageData() throws IOException {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int BUFFER_SIZE = 1024*16;
byte[] imageData;

    // download the file
    Log.d(LOG_TAG, "fetching image " + imageUrl);
    BufferedInputStream istream = new BufferedInputStream(connection.getInputStream());
    ByteArrayOutputStream  out = new ByteArrayOutputStream(BUFFER_SIZE);
    byte[] tem = new byte[BUFFER_SIZE];
    int bytesRead = 0;
    bytesRead = istream.read(tem);
    while(bytesRead != -1){
        out.write(tem, 0, bytesRead);
        //Log.d(AsyncImageLoader.class.getName(), "size:"+out.size()+",url:"+imageUrl);
        bytesRead = istream.read(tem);
    }

    if (out.size() != 0){
        imageData = out.toByteArray();
    }
    out.close();
    istream.close();
    connection.disconnect();

    return imageData;
} 

Sorry I am not familiar with Git, thus the coding part looks strange, i don't know why =_=??

@mbthoora
Copy link

This issue also crops up when images are gzipped by the server. The content-length header (at least in the installation of Apache I'm working with) is the length of the compressed data, and the download code breaks on it.

The solution proposed here works, but unfortunately duplicates the image data in the call to out.toByteArray().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants