0

I'm having some problems with loading an image in a JEditorPane by using the tag. I get the body of the mail from Gmail, which looks like this:

<div dir="ltr"><div><img src="cid:ii_lagwgzau0" alt="test.jpg" width="451" height="338"><br></div><br></div>

I edit the line via code to look like this:

<div dir="ltr"><div><img src="file:///cache/test.jpg" width="451" height="338"><br></div><br></div>

I save the image in the folder /cache, at the same level of /src, but the image just won't load no matter what I try. I also can't figure out if its a path problem or my approach just doesn't work, and I can't find anything that helps online. Anyone knows what I'm doing wrong here?

This is what it looks like graphically:

enter image description here

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • *I save the image in the folder /cache,* That means a directory off the root of your filesystem. I doubt that's where it is. I would try `./cache` but you might need to try a different mechanism – g00se Jan 23 '23 at 20:06
  • The/cache folder is in the project – Giovanni Grana Jan 23 '23 at 21:20
  • I'm aware it's in the project. Running software doesn't care about your project. In fact you shouldn't either, because the project is an IDE thing and nothing to do with proper running software, which must be cut loose and be able to fend for itself outside your IDE. If you use that html markup, it necessarily is using the filesystem rather than the classloading mechanism, which is a great weakness. If you want a simple, but ultimately weak, way to get that working, in place of 'cache' you must place the full and absolute path to that directory after the protocol `file:` – g00se Jan 24 '23 at 00:16
  • The [classloader approach](https://stackoverflow.com/questions/15311188/inserting-images-in-jeditorpane-using-pane-getdocument-insert) – g00se Jan 24 '23 at 00:25

1 Answers1

1

Remove the three forward slashes between "file:" and the path. Change

<img src="file:///cache/test.jpg">

to

<img src="file:cache/test.jpg">

Found it from this answer: https://stackoverflow.com/a/10084395/7376577

The file structure:

root
├───cache
│       test.jpg
│
└──src
        MyJFrame.java
Peter F
  • 137
  • 8