Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java [Java] PNGException

  1. Sep 22, 2012 #1
    I'm getting this error

    sun.awt.image.PNGImageDecoder$PNGException: crc corruption
    at sun.awt.image.PNGImageDecoder.getChunk(Unknown Source)
    at sun.awt.image.PNGImageDecoder.getData(Unknown Source)
    at sun.awt.image.PNGFilterInputStream.read(Unknown Source)
    at java.util.zip.InflaterInputStream.fill(Unknown Source)
    at java.util.zip.InflaterInputStream.read(Unknown Source)
    at java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read1(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at sun.awt.image.PNGImageDecoder.produceImage(Unknown Source)
    at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
    at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
    at sun.awt.image.ImageFetcher.run(Unknown Source)

    This is the segment of code causing the error.

    Code (Text):
    URLConnection con = new URL("www.example.com").openConnection();
            InputStream is = con.getInputStream();
            byte bytes[] = new byte[con.getContentLength()];
            BufferedInputStream bis = new BufferedInputStream(is);
            Toolkit tk = getToolkit();
            File save = new File("picture");
            FileOutputStream fos =  new FileOutputStream(save);
            if(tk.prepareImage(image.getImage(), 900, 900, null)){
    The file i output to shows that the image is corrupt, only a few lines of the image are visible and the rest is black. tk.prepareImage() also returns false. This segment sits in a method called once in its class's constructor method. I'm contacting the maps.google server for a map image with the proper parameters. The image returns fine with the same url in a browser . I've checked the google maps API documentation but nothing stands out. Any ideas?

    Edit: Attempted requesting a different image format, but now i get "Premature end of JPEG file"
    Last edited: Sep 22, 2012
  2. jcsd
  3. Sep 22, 2012 #2


    User Avatar
    Homework Helper
    Gold Member

    This seems like a really convoluted way to grab an image from a URL. If the URL contains nothing but the image, why not just use something like
    Code (Text):
    try {
        Image image = tk.createImage(new URL("www.example.com"));
    } catch (//whatever exceptions URL's constructor & ToolKit.createImage(URL u) throw) {
        //handle exceptions
    Last edited: Sep 22, 2012
  4. Sep 23, 2012 #3
    I figured it out. Your way is a lot easier, I'm just used to doing things on a much lower level (C programmer). The stream reader would only read what was available() into the array and not the full contentLength() but a while check sum loop read the full content.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook