How to change FileInputStream image to image from resources? [duplicate] - java

This question already has an answer here:
How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?
(1 answer)
Closed 2 years ago.
I have this line:
//Creating an image
Image image = new Image(new FileInputStream("C:\\images\\image.png"));
I would like to change to this line, I would like to get the image from resources in IntelliJ and not from my PC's C drive, for example, but it doesn't work:
Image image = new Image(new FileInputStream("file:src/main/resources/images/image.png"));
or
Image image = new Image(getClass().getResource("file:src/main/resources/images/image.png").toExternalForm());
I have an exception:
Caused by: java.lang.RuntimeException: Exception in Application start method

The path is determined from the classpath (= root) which is the case of src/main/resources.
Thus
Image image = new Image(getClass().getClassLoader().getResourceAsStream("/images/image.png"));
...should work.
You get get some more details here.

You simply need do like this.
In project put your images:
ProjectName/image.png
example of the project
and then
Image image = new Image(new FileInputStream("image.png"));

Related

JavaFX image pathway issues

I have just figured out how to unzipped a zippedfolder into the folder EBookReader/books from within my program (where EBookReader is my project). Now I have a /books/testbook/1.png (what was unzipped) however no matter what I do I can not get the program to display an image on that pathway.
System.out.println(loadedBook.returnPage());
Image page = new Image(getClass().getResourceAsStream(loadedBook.returnPage()));
ImageView imagePage = new ImageView();
imagePage.setImage(page);
imagePage.setFitWidth(350);
imagePage.setFitHeight(500);
imagePage.setPreserveRatio(true);
imagePage.setSmooth(true);
imagePage.setCache(true);
border.setCenter(imagePage);
loadedBook.returnPage() returns the string "F:\EbookReader\books\testBook\1.png" which just so happens to be the location of the image 1.png and even if I enter the string manually into the image location it still doesn't work. I heard I didn't need the getClass() junk but it doesn't work without that either. The only way I have gotten it to work is if I put the image in directly in F:\EbookReader\src\ebookreader.
EDIT: The error is that the input stream is null!
getResourceAsStream() is expecting a location either relative to the current class, or relative to the classpath. The path you showed certainly is not relative to either of those.
I recommend using the Imageconstructor taking a URL and doing
Image page = new Image(new File(loadedBook.returnPage()).toURI().toString());
You could also do
Image page = new Image(new FileInputStream(new File(loadedBook.returnPage())));

Java applet not able to find resource image with ImageIcon unless entire path is used

I've placed the image TestIcon.png in the src folder of a Java applet (the directory is C:\Users\User\workspace\applettest2\src\TestIcon.png). Everything I've read says that I should be able to use the image simply by referencing "TestIcon.png". Example:
ImageIcon xIcon = new ImageIcon("TestIcon.png");
Howerver, running the applet in Eclipse with this code doesn't work; the image is not displayed. If I type out the image's entire path:
ImageIcon xIcon = new ImageIcon("C:\\Users\\User\\workspace\\applettest2\\src\\TestIcon.png");
then the applet displays the image properly. Isn't the default path for resources supposed to be in the "src" folder? Thanks.
If you are running it inside the Applet then try with Applet#getCodeBase() and Applet#getDocumentBase
Image image = getImage(getDocumentBase(), "TestIcon.png");
Find more samples Here and Here

Read image file from same package [duplicate]

This question already has answers here:
Including Images with an executable jar
(2 answers)
Closed 9 years ago.
I want to read a bunch of image files from my current package because I want to get access to the files when my package which includes my image files is exported to others' computer.
I just want to make sure my program can read the images no matter if the package is in my computer.
I tried
File file = new File("images.jpg"); // It is wrong because the path is wrong.
// I want to assign the image as BuffuredImage
BufferedImage dealerCardImage1 = ImageIO.read(file);
I was wondering how the path of the files should be. What should I do?
put your image file inside the package of current class file and try this:
BufferedImage dealerCardImage1 = ImageIO.read(getClass().getResourceAsStream("images.jpg"));
Note that, getClass().getResourceAsStream(path) returns an InputStream that points to a path that starts from current package.
For example if you have a class file named HelloWorld inside package com.example, then HelloWorld.class.getResourceAsStream("images.jpg") returns an InputStream to image with this path: com/example/images.jpg
if you read from file use
Image image = new Image();
image = ImageIO.read(file);
// Read from an input stream
InputStream is = getClass()
.getResourceAsStream("/com/statement/SamplePDFStatementFile.txt");
image = ImageIO.read(is);
//if you read from url
// Read from a URL
URL url = new URL("http://hostname.com/image.gif");
image = ImageIO.read(url);
ImageIO.read(file); will return null if no registered ImageReader is not found.
Please check whether you have registered any ImageReader or not.
I think this code snippet could help you
File file = new File("images.jpg"); // Assuming images.jpg in my working directory
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis); //reading the image file
You just need to wrap the file into an FileInputStream and then pass it to read()

Load image in jar, error in code

I want to reference an image in my project that I will package into a Jar file.
This code is not working:
ImageIcon image = new ImageIcon("Images/buttonBackgroundSelected.png");
Any ideas? thanks
This constructor only works if the image is available in the file system outside the JAR file
new ImageIcon("Images/buttonBackgroundSelected.png");
You almost never want to do this. You could use:
ImageIcon imageIcon =
new ImageIcon(getClass().getResource("/Images/buttonBackgroundSelected.png"));
when the folder and image and the have been included in the JAR file.
However, loading images this way fails silently if any issues loading the image. Therefore use ImageIO.read should be used:
Image image =
ImageIO.read(getClass().getResource("/Images/buttonBackgroundSelected.png"));
The resultant image can be wrapped in an ImageIcon if required.

How do I access a .gif file in a package in the src folder?

I'm working on a Java program using Eclipse. Right now, I have an src folder that contains 2 packages: memory.views and memory.resources.
In the memory.views package, I have my Main.java file. In the memory.resources package, I have my .txt file and .gif file.
Within the program, I have no problem accessing (and manipulating) the .txt file by using the path /memory/resources/name.txt. However, when I do the same with the .gif file using the code below, I get no result:
ImageIcon icon = new ImageIcon("/memory/resources/name.gif");
There's no error produced. The only effect is that I see no image when the program is running.
I've tried also writing the following, but none worked:
ImageIcon icon = new ImageIcon("/resources/name.gif"); <br>
ImageIcon icon = new ImageIcon("name.gif");
Now, just so nobody says that it's the .gif file's fault, I've actually entered in the full Finder path (I'm using a Mac) and that worked perfectly:
ImageIcon icon = new ImageIcon("/Users/[...]/src/memory/resources/name.gif");
However, I don't want to do the full path, because if I export the program and run it on another computer, then the non-programming computer won't display the image either.
So, right now, I don't even know what the problem is. The .gif file works sometimes, but not when it's accessed via the same path as the .txt file, which works all the time. I tried looking here (Cannot access resource by using the getResources() method), but it seems like he had a slightly different problem from me.
You can use
URL url = ClassLoader.getSystemResource("name.gif");
ImageIcon icon = new ImageIcon(url);
provided that your name.gif file ends up in classpath after compilation/build.
What is this ImageIcon class? Is it your own code?
Try Thread.currentThread().getContextClassLoader().getResourceAsStream("/memory/resources/name.gif");
See if this works...
File file = new File("/memory/resources/name.gif");
ImageIcon icon = new ImageIcon(file.getAbsolutePath());

Categories