I am having trouble reading an image. If I do the following
URL url = new URL(\"http://tctechcrunch2011.files.wordpress.com/2012/10/gmm.jpg\");
ImageInputSt
The problem maybe related to ImageIO.read that fails to correctly read some JPG images. Here is a similar bug (Bug ID: 4881314) that may still be partially unresolved.
As an alternative you can try using Toolkit.createImage that seems to handle the specified image correctly. For example:
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class TestImage {
public static void main(String args[]) {
try {
URL imageUrl = new URL(
"http://tctechcrunch2011.files.wordpress.com/2012/10/gmm.jpg");
BufferedImage ioImage = ImageIO.read(imageUrl);
Image toolkitImage = Toolkit.getDefaultToolkit().createImage(
imageUrl);
JPanel panel = new JPanel();
panel.add(new JLabel(new ImageIcon(ioImage)));
panel.add(new JLabel(new ImageIcon(toolkitImage)));
JOptionPane.showMessageDialog(null, panel, "ImageIO vs Toolkit",
JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Failure",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
Here is the result:
