Charan February 2016

Java isn't reading a JPG image where as a photo viewer can read it

I have a scraped few images from a website. The scraper did not download the image files with the correct extension.

I changed one of the file's extension to ".jpg" and it opened in Picasa. So I wrote a Java program to change all downloaded image file extensions to ".jpg".

Now when I tried to open images in Java, using Swing, it's not displaying the image. I am using this code to display. It is working fine with other images downloaded manually from web.

String path = "resources\\images\\"+gamePlayer.getName()+".jpg";        
JLabel image = new JLabel(new ImageIcon(path));

I even tried converting image files to a different format by a third part software(Format Factory) but even it couldn't read the file. I can open and view all the images in Picasa.

How do I display scraped images with incorrect extensions using Swing ?

Answers


DaveRlz February 2016

Picassa is probably inspecting the file signature (https://en.wikipedia.org/wiki/List_of_file_signatures) rather than the file extension and therefore opens it correctly. You can't just rename a file and expect your code to understand how to open/display it.

Post Status

Asked in February 2016
Viewed 1,001 times
Voted 14
Answered 1 times

Search




Leave an answer