2

I've got an issue with saving a BufferedImage using a simple paint program. When I save the image from the paint such as a picture of a snake that I drew earlier, it saves the image just fine, but it doesn't save it in the way you might think. Instead of saving the image to a C:\ drive (or whatever drive a user may be using) it saves the image to the eclipse workspace. This of course is unacceptable, as this needs to be given directly to a user's main place of access is. Here is the method that is used for saving the bufferedimage.

static void saveImage() {
        try {
            ImageIO.write(background, "png",
                    new File(fileName + ".png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

So here, background is obviously the image being saved, png is the extension, and fileName is a string that is saved earlier using a prior method that isn't important here. This method saves the image to the eclipse workspace. This is bad. We need this to save to the default drive. How do I accomplish this? Let me know if you need anything else to aid you with your answer.

EDIT: So, as requested, here is the code that changes the fileName. It is in a different class with a differnt UI completely, and because I'm not sure of how much to post, I'll post the actionListener and the getName() method. What happens here is that there is a JTextField that, once a JButton is pressed, has the string extracted from it and uses it as the fileName. Here's the code:

`finishButton.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   ProgramUI.fileName = getName();
                   ProgramUI.fileHasName = true;
                   ProgramUI.saveImage();
                   frame.dispose();
               }
        });
    }

    public String getName() {
        return nameField.getText();
    }
`
user2398233
  • 137
  • 1
  • 2
  • 10
  • *This is bad*. The problem is in `fileName` value, which you say *is saved earlier using a prior method that isn't important here*. Well, **it is important** to help you answer this question. Please provide how you *save* this variable. – Luiggi Mendoza Jun 09 '13 at 03:03
  • @LuiggiMendoza As requested, question edited. – user2398233 Jun 09 '13 at 03:13
  • The workspace _is_ on your c: drive. You need to understand the concept of absolute and relative path names and the concept of current working directory as set in your launch configuration. – Thorbjørn Ravn Andersen Jun 09 '13 at 06:42

3 Answers3

4

Offer the user a JFileChooser (as seen in this answer) to allow them to navigate to a path and select a name.

Restrict the path using a FileSystemView as seen here.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

If that code is saving to an Eclipse workspace, it is because:

  1. you are supplying a relative pathname in filename, AND
  2. the current directory is the Eclipse workspace directory when you run the application from within Eclipse.

Try running the command from the command prompt. With the same inputs (i.e. the same filename as you are currently using) it should save to the current directory.


It is not possible to give you a simple recipe for solving this problem. The correct solution depends on how you want the program to actually work:

  • Where (i.e. in what directory) you want the file to be saved.
  • Whether (and how) you want the user to be able to say where to save the file.
  • Whether this application needs to be portable; i.e. work on something other than Windows.

I suggest that you start by reading up on the concept of "the current directory", and read the javadoc for the java.io.File class ... which will explain how Java decides what file you "mean" when you try to open one.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

You have not given the path to save the file when you are creating a File object. By default it will save the file in the current working directory.

Try this :

static void saveImage() {
        try {
            ImageIO.write(background, "png",
                    new File("C:\\" + fileName + ".png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

or better add a path parameter to the method:

static void saveImage(String filePath) {
        try {
            ImageIO.write(background, "png",
                    new File(filePath + fileName + ".png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

but make sure that you get the path with proper slashes (//)

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136