It works if you do in two steps: add extension to selected file, then create File
object with the extension appended to it.
String withExtension = chooser.getSelectedFile().getAbsolutePath() + ".jpg";
File file = new File( withExtension )
Implied in your question, but just to cover all bases: need to check if the extension isn't already there first, e.g.:
String withExtension = chooser.getSelectedFile().getAbsolutePath();
if( !withExtension.toLowerCase().endsWith( ".jpg" ) )
withExtension += ".jpg";
[You may want to add ".jpeg" to the if
above - it's a valid extension for JPEG files]
The code below may or may not work. File
has a toString()
that will allow it to compile, but I'd rather use the method above, where I can control the exact path name I'm getting. The toString()
documentation for the File
object is a bit unclear to me on what exactly it returns. In cases like this one, I prefer to call a function that I know returns what I need for sure.
File file = new File( chooser.getSelectedFile() + ".jpg" );