MavidDeyers
MavidDeyers

Reputation: 290

JavaFX HTMLEditor - Inserting local Image with absolute path

i'm adjusting the HTMLEditor in JavaFX and my goal is to insert local images in the HTMLEditor. Images on the Internet are no problem and work fine but when I try to insert local images with an absolute path then it just shows the famous "X", that he didnt find it.

Tried many things, my current idea is shown in the following code:

htmleditor.setHtmlText(
"<img src='file://C:/hi.png'/>");

and this leads to the following html code, which is fine i think and the result i wanted but its not rendering the image...

<html dir="ltr"><head></head><body contenteditable="true">
<img src="file://C:/test.png"></body></html>

2nd idea) I also generated a picture and saved it on my hard disk, called the file with the absolute path, but this leads to the same problem as above (and here im expecting the path to be 100% right...).

File file = new File(TextArea_imagePath.getText() + ".png");
ImageIO.write(img, "png", file);
htmleditor.setHtmlText("<img src=' " + file.getAbsolutePath() + "'/>");

Hope anyone can help me, guess its a stupid mistake.

p.s: Set a local image in JavaFX HTMLeditor couldnt help me and treated images without path

Upvotes: 0

Views: 517

Answers (1)

VGR
VGR

Reputation: 44338

Your URIs are incorrect. After the file: part (the scheme), you must have either one slash or three slashes. You must not have two slashes.

The following are valid:

  • <img src='file:/C:/hi.png'/>
  • <img src='file:///C:/hi.png'/>

The following is not valid:

  • <img src='file://C:/hi.png'/>

The reason for this is that the URI syntax gives special meaning to two slashes following a scheme. From the URI generic syntax specification:

This "generic URI" syntax consists of a sequence of four main components:

<scheme>://<authority><path>?<query>

So you can either omit the //<authority> entirely, or you can specify an empty authority, as the first two examples above do.

Two slashes would mean the text that follows, C:, is an authority—that is, a host name with optional user, password and port number. Obviously C: is not a valid host name, and even if it were, the precise meaning of a host name in a file: URI is not well defined.


Your second idea will not work as it is. A file name does not automatically constitute a valid URI. You need to convert it to a URI:

htmleditor.setHtmlText("<img src=' " + file.toURI() + "'/>");

Upvotes: 1

Related Questions