Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JChooser save ---> Works but Can't see the saved image in the folder

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

    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?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    976
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default 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:

    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!)

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. How to save Image into a package by JButton save?
    By justyStepi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2012, 08:02 PM
  2. Create image Jpeg from an object of Image class.
    By Ramandeep in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 31st, 2011, 11:34 PM
  3. [SOLVED] what data type should i use in MySql for a java object to be saved
    By chronoz13 in forum JDBC & Databases
    Replies: 4
    Last Post: October 9th, 2011, 08:17 AM
  4. Save JPanel as image
    By fahien in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2011, 08:20 AM
  5. JFileChoose and FileOutputStream, nothing is being saved
    By lost in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 27th, 2010, 09:10 PM