0

My workstation is running windows, and we develop Java Applications that run on windows. On these windows desktops we have access to our shared network drive. So I need to load an image from the network drive, but it doesn't work. Am I doing this wrong?

import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class LoadImage {
    public static void main(String[] args) {
        try  {
            Image image = Toolkit.getDefaultToolkit().getImage("\\\\networkdive\\folderonnetwork\\image.jpg");  
            JOptionPane.showMessageDialog(null, "", "", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(image));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
samwell
  • 2,757
  • 9
  • 33
  • 48

3 Answers3

2

If you are encountering significant network latency, you can defer the loading to an instance of SwingWorker, as shown in this example.

Note that the example uses ImageIO.read() rather than Toolkit#getImage(), which "may still contain stale information which was loaded from the file after a prior call."

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I did this: `Image image = ImageIO.read(new URL("//NetworkDrive/Folder/image.jpg"));` and I got the error, "java.net.MalformedURLException: no protocol: //NetworkDrive/Folder/image.jpg" – samwell Aug 31 '12 at 20:18
  • What not use `new File()`, as shown [here](http://stackoverflow.com/a/1682450/230513)? – trashgod Aug 31 '12 at 20:40
  • 2
    @chudapati09 try new File("\\\\networkdrive\\folder\\image.jpg") the path you tried to use is not a valid URL, urls should start with a protocol identifier. I've just spent the last week writing a picture API to read pictures off network drives this way & this works just fine – MadProgrammer Aug 31 '12 at 21:17
2

I don't think a Samba/CIFS path is a valid URL or a local file. Have a look at JCIFS instead.

martijno
  • 1,723
  • 1
  • 23
  • 53
  • Interesting. On my platform Samba/CIFS shares appear under a mount point named `/Volumes/`. Is there nothing similar on Windows? – trashgod Aug 31 '12 at 21:07
  • Yes, on Windows you can map a share to a drive letter, say `U:`. Files can then be accessed from Java using `new File("u:/filename.txt)`. Requires OS level user interaction though (just like mounting on Unix). – martijno Sep 01 '12 at 06:06
  • Ah, that makes sense; JCIFS would allow programatic access to the share. +1 and thanks for the explication. – trashgod Sep 01 '12 at 13:05
0

I had the same problem and found this through Google. I resolved this issue by using "file:\\\\" ahead of the samba url.

Saurfang
  • 685
  • 7
  • 14