0

My program allows you to save an image that has been made. I only have to save it as a png.

I currently have:

...
File file = fc.getSelectedFile();
try{ ImageIO.write(result.getImage(),"png",file);}
...

It saves the image and I am able to open it back up in my program, BUT it doesn't show the .png at the end of its name. The sample images (given to us, not made by the program) show .png on my JLabel, so I know its something to do with how i'm saving them.

I have tried the answer here but that didn't work.

How do I fix this?

(and side question: what is the point of even putting "png" if write is just going to ignore it?)

Thanks!

Community
  • 1
  • 1
MarvelFan
  • 11
  • 1

1 Answers1

2

From the documentation:

Writes an image using an arbitrary ImageWriter that supports the given format to a File. If there is already a File present, its contents are discarded.

...

formatName - a String containg[sic] the informal name of the format.

So "png" as the second argument is telling ImageIO how to encode the file, not the file extension. You could go into MS Paint and save an image as .jpg, and then rename the file to .png, but it would still be a .jpg on the inside; the .png extension is just a signal (to the user, really) of the type of file that it is.

The answer you linked to looks like it should work. What do you mean by "doesn't work"? Do you get an error? A warning? Does it just not add the name?

Also, try checking whether your file name extensions are shown (in Windows: Tools menu → Folder options... menu item → View tab → Deselect "Hide extensions for known file types" checkbox; on Mac, I think it's in Finder menu → Preferences... menu item).

wchargin
  • 15,589
  • 12
  • 71
  • 110
  • it just doesn't add the .png to the filename when it is printed out in my JLabel elsewhere in my program. It runs without errors. I'm doing this through a Linux terminal... but when I go to open an image in my program, it still doesn't show the extension while the ones given to us do. – MarvelFan Mar 24 '13 at 05:42