Christian
Christian

Reputation: 51

get resource path

created a new folder "resources" with all my images in eclipse. I added this folder to the class path.

Now i tried to access this images

URL url = new URL("imageA");

I also tried

URL url = new URL("resources/imageA");

Both doesnt't work. What is wrong?

Sincerely Christian

Upvotes: 5

Views: 23774

Answers (4)

1218985
1218985

Reputation: 8012

You can use something like this if you are trying to load an image from a local location.

File file = new File("D:/project/resources/imageA.jpg");
Image image = ImageIO.read(file);

If you are planning to read the image from a URL, use the below code:

URL url = new URL("http://www.google.co.in/images/srpr/logo3w.png");
Image image = ImageIO.read(url);

Please have a look at the below code for better understanding:

package com.stack.overflow.works.main;

import java.awt.Image;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * @author sarath_sivan
 */

public class ImageLoader {

    private ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    private Image image = null;
    private URL url = null;

    /**
     * Loading image from a URL with the help of java.net.URL class.
     */
    public void loadFromURL() {
        image = null;
        try {
            url = new URL("http://www.google.co.in/images/srpr/logo3w.png");
            if (url != null) {
                image = ImageIO.read(url);
            }

        } catch(Exception exception) {
            exception.printStackTrace();
        }
        display(image, "Loading image from URL...");

    }

    /**
     * Loading image from your local hard-drive with getResource().
     * Make sure that your resource folder which contains the image
     * is available in your class path.
     */
    public void loadFromLocalFile() {
        image = null;
        try {
            url = classLoader.getResource("images.jpg");
            if (url != null) {
                image = ImageIO.read(url);
            }

        } catch(Exception exception) {
            exception.printStackTrace();
        }
        display(image, "Loading image from Local File...");
    }



    private void display(Image image, String message) {
        JFrame frame = new JFrame(message);
        frame.setSize(300, 300);
        JLabel label = new JLabel(new ImageIcon(image));
        frame.add(label);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        ImageLoader imageLoader = new ImageLoader();
        imageLoader.loadFromURL();
        imageLoader.loadFromLocalFile();
    }

}

Output

enter image description here

enter image description here

Upvotes: 1

kgiannakakis
kgiannakakis

Reputation: 104168

See here for directions. You can't load an image directly with URL (actually you can, but it is complicated see this question)

Actually you need to do something like this:

ClassLoader loader = ClassLoader.getSystemClassLoader();

if(loader != null) {
   URL url = loader.getResource(name);
}

Upvotes: 1

Chuk Lee
Chuk Lee

Reputation: 3608

If you are loading from your classpath you should do something like this

InputSteam is =  this.getClass().getClassLoader().getResourceAsStream("resources/myimage.png")

Upvotes: 3

Related Questions