JChooser save ---> Works but Can't see the saved image in the folder
Hello,
I have written a code to upload an Image Icon to Jlabel by JChooser and save Image Icon. "Both of them work".
But when I save the Image to given location I cannot see it after. Can you please help me to find the mistake?
Code :
JButton OpenIcon = new JButton("Open New map");
OpenIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser chooser = new JFileChooser();
chooser.addChoosableFileFilter(new ImageFileFilter());
int returnVal = chooser.showOpenDialog(null);
if(returnVal ==JFileChooser.APPROVE_OPTION){
File file =chooser.getSelectedFile();
Fpath = file.getPath();
ImageIcon Sicon = new ImageIcon(Fpath);
pic.setIcon(Sicon);
pic.repaint();
}
}
});
JButton Save = new JButton("Save New Map");
Save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
Sicon = (ImageIcon) pic.getIcon();
bi = new BufferedImage(Sicon.getIconHeight(),
Sicon.getIconWidth(),
BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
fill = new File("outputFile");
JFileChooser chooserS = new JFileChooser();
chooserS.addChoosableFileFilter(new ImageFileFilter());
chooserS.setSelectedFile(fill);
int returnVal = chooserS.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION){
ImageIO.write(bi, "JPG", fill);
File filr =chooserS.getSelectedFile();
System.out.println("Save as file: " + filr.getAbsolutePath());
}else{
System.out.println("Saving was cancelled by user.");
}
}catch(IOException io){
io.printStackTrace();
}
I load the Image by browse JChooser, it loads and it's there. Then I want to save the Image which I just loaded. I have converted the Image Icon into BufferedImage so I can save it.
When it's saved I get messaged that the Image was saved into some folder. But when I open the given folder it's not there. Can you please help me to find why it's not working?
Re: JChooser save ---> Works but Can't see the saved image in the folder
In the save button's event handler you create a new (empty) BufferedImage, bi. And you create a graphics context, g, based on that image. But, unless I'm missing something, you don't actually use that graphics to draw anything into your image.
Also ImageIcon has a getImage() method that might, more easily get at the image you want to save.
-----
This doesn't actually address your problem! "But when I open the given folder it's not there." Print the actual file you use as the destination:
Code :
ImageIO.write(bi, "JPG", fill);
System.out.println("written to " + fill.getAbsolutePath());
File filr =chooserS.getSelectedFile();
// etc
Also try printing the results of exists() on fill. (And check the console for any IOException!)
Re: JChooser save ---> Works but Can't see the saved image in the folder
Cool thx ur post actually helped me. I found that little hidden mistake; :)
Thx
It was in : File filr =chooserS.getSelectedFile();
ImageIO.write(bi, "JPG", filr);
System.out.println("Save as file: " + filr.getAbsolutePath());
I had to use different File:)>-