I have the following piece of source code
import java.net.URI;
import java.net.URISyntaxException;
File file = new File("pic.png");
BufferedImage image = ImageIO.read(file);
String string = "pic.png";
//the code works fine until here
URI path = new URI(string);
File f = new File(path);
ColorProcessor image = new ColorProcessor(ImageIO.read(f));
So the path that the File gets is correct. Image is buffered correctly also. Now my problem is that i'm getting the following exception
Exception in thread "main" java.lang.IllegalArgumentException: URI is not absolute
at java.io.File.<init>(Unknown Source)
Why is my path not absolute?And how do i do it right?
If i change the path like this:
String string = "C:'\'Users'\'Jurgen'\'newFile'\'myProject'\'pic.png";
Also tried like this
String string = "C:/Users/Jurgen/newFile/myProject/pic.png";
Then i get a new exception
Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
P.S. not working with android packages for uri
Thanks in advance=)
Solution 1If a relative url is sent to a rest call in the RestTemplate, the relative url has to be changed to an absolute url. The RestTemplate will use the absolute url to identify the resource. In the example below, the complete url “http:/localhost:8080/student” is used to invoke a rest call using RestTemplate.
May 18, 2022•Knowledge 000127381 The error URI is not absolute as seen when the protocol (HTTP/HTTPs) is not defined for the URI in properties. This issue is caused due to the missing URI protocol http/https in any URI property of the configuration.
File getAbsolutePath() method in Java with Examples The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object. If the pathname of the file object is absolute then it simply returns the path of the current file object.
Uri uri = data. getData(); File file = new File(uri. getPath());//create path from uri final String[] split = file. getPath().
You are trying to create uniform resource identifier, but the name should follow the url conventions. This means, that it's necessary to provide a scheme (take a look here, to see all available schemes). So, in your case, you have to create the URI with string, like file:/pic.png
or may be some other scheme.
As for your full path, it could be done like:
String string = "file:/C:/Users/Jurgen/newFile/myProject/pic.png";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With