I have a javafx app that allows selecting an image and displaying it or lets you select a folder, read images inside and display them.
In either case, it works fine on Windows, but when run on macOS it does not work and throws a FileNotFoundException
.
Here is the code to read it:
String path = Paths.get(pathToFile).toUri().toString()
That line gives me the path
file:///Users/userName/Downloads/untitled%20folder/image.png
But then the exception says
file:/Users/username/Downloads/untitled%20folder/image.png (No such file or directory)
I tried to remove the space from the folder, give 0777 permission to all files. It just does not read it.
I want to add that the file path is obtained from the javaFX file chooser and it is not put there manually.
The complete code is pretty much this:
String path = Paths.get(loadedFilePath).toUri().toString();
File file = new File(path);
System.out.println(file.exists());
com.drew.metadata.Metadata metadata = ImageMetadataReader.readMetadata(new File(path));
The exception is thrown in the readMetadata()
method but already the file.exists()
returns false.
The File
class works with local paths, not URIs, so going through Paths
just to get a URI and then convert it to a string and use it with a File
won't work, as you've seen.
Instead, you can use Paths
to get a File
object directly:
File file = Paths.get(loadedFilePath).toFile();