Can not display local image file as WebEngine imag

2019-08-11 09:18发布

问题:

I'm trying to set the src of an img tag I have dynamically. The following piece of code works when running from eclipse but not after exporting it as a running jar file:

doc.getElementById("user-thumb").setAttribute("src", selectedVcard.getThumb().getFilePath());

The element got by the "user-thumb" id is an image. The thumb object returned is a simple custom ImageView I've extended to hold more information and is initialized using:

public Thumb(String url) {
    super(url);
    this.setFilePath(url);
    getStyleClass().add("thumb");
}

the method getFilePath() is returning a path from a temporary file starting with:

file:/

But I've already tried to change it to

file://

and even:

file:///

and got no success. I have googled and searched here but every answer points to start with file://. Is there something wrong with my code or is it javafx 2? By the way, I'm using javafx 2.2 GA and the jre 1.7.0.6 from oracle. Cheers

回答1:

You could use Data URI for the images

Something like ...

String imageMimeType = "image/jpeg"; // Replace this for the correct mime of the image
String dataURI = "data:" + imageMimeType + ";base64," + 
     javax.xml.bind.DatatypeConverter.printBase64Binary(imageByteArray);