1

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

Bruno Vieira
  • 3,884
  • 1
  • 23
  • 35
  • is this image located inside jar? – Sergey Grinev Sep 05 '12 at 22:31
  • No, it's a file passed via socket and stored as a temporary file in the user (client) machine. Note please that in my Thumb (ImageView) the image is displayed perfectly, the WebView will even show the image if I use the ContextMenuItem Show Image in another window, it just won't display it. It's starting to seem like a bug – Bruno Vieira Sep 05 '12 at 23:36
  • I guess you are running as a click to run jar not webstart or an applet? (otherwise security restrictions could cause this behaviour). I also guess that the image cannot be loaded from a relative path to the source document? (otherwise you may be able to workaround this by using the relative path). – jewelsea Sep 05 '12 at 23:48
  • I tried running as a click first and then realized it could generate this security restriction, so I'm using the console even under Windows 7.This is a cross-platform application where the server is under a Unix environment and the client is working on Linux, MacOsX and Windows. I can't use relative path since it's a temporary file, which means it has it's path provided by the system. – Bruno Vieira Sep 05 '12 at 23:58
  • There is better and easier solution at [here][1]. [1]: http://stackoverflow.com/questions/8923801/how-to-reach-css-and-image-files-from-the-html-page-loaded-by-javafx-scene-web-w –  Jun 22 '14 at 08:22

1 Answers1

2

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);
mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62