How do you open local project images with ImageIO.read()? - java

I've been at this for 20 minutes now and I just can't wrap my head around where I'm going wrong.
If an image is in the same src folder as a java file, why doesn't this work:
Image image = ImageIO.read(getClass().getResource("/image.png"));
I'm not sure if I'm missing a trick or something but I've only been able to load image from http URLs which isn't very helpful for my project.
I know this is a silly question but I've looked at many other answers with similar titles but very different problems. There was also another answer that didn't work at all.
I feel like an idiot posting this but I'm honestly about to upload all my resources to a cloud service instead of having to deal with this.

after this interesting discussion , we found that the image file was corrupted and that is why it was not reading (parsing) it to BufferedImage properly

it should be in the same folder as your .class file not .java file! and furthermore you as others commented you should remove / from image address

Related

Get files using TreeView library

First of all sorry my English is not good I will accept any edits of my question, this is the library link.
I have already test the library and worked well, This is the result.
But I need to get files located in storage path /storage/emulated/0/ to a treeview recyclerview something like this.
Codes
Since the codes are large I cant post them here so the codes are same located in this link. Thank you for your time.

Questions about loading files in a program

Sup!
I'm trying to understand the concepts and differences between them to load and save files, and trying to figure out the best way to load files with a program I'm making (a database). I'm using Java, but try to help me in a more general way, if possible. Whatever help I get is a lot welcome and I thank you already for it.
I know there is "File Reader" in Java and a lot of programs use files in .txt format. I tried to open a .doc file with Notepad, just to see what would happen, and NOTHING was a text. I expected that, not everything though.
I just don't know how to make a file that is not a .txt. How that happens?
This is a big one. When I load a file, should I load all its content and let it ready for use, or should I just keep the file path and read from it as I use, using offsets and pointers starting from the file path? How should I do this either way?
In this database program of mine, some things will be images (for example), and if I were to use a .txt, I would give each image a label that the program would read and then interpret it like the label of the image, and then get the image. How could I do that?
I was sure I had more questions. If I do I ask again.
Once more, thanks in advance for the attention and help.
When it comes to the .txt bit, all "plain text" will work. Just change the file extension to .ini or .sql (if sql is installed) and change it in your filepath too.

Tutorial on how to import/read a tif/tiff file using java or the java advanced imaging API

I have a tiff image with many directories and subdirectories. I would like to navigate this tiff directory tree and import the image into a 2D array, so I can process this image. I am relatively new to java and cannot figure out how to do this. Will someone please reply to my question with a tutorial and examples of code?
Also, does anyone else find the java API documentation difficult to find, difficult to navigate, and somewhat sparse? I am trying to do this as a workaround for a processing sketch I am creating. Are there different workarounds?
Thank you.
Well i didn't even know a tiff could have directories and sub directories... But I would have a look in java File class as a start point to try to see those directories from your app. If you can see them, than you probably can parse them too.

Spring 3 mvc how to upload an image to web app resources

I have a problem with an image upload in spring 3 mvc. I search a couple of hours but can't find any satisfying solution.
What I want to do?
I simply want to upload an Image to the server and store it in a location where I can easily access it from my jsp sites to show the image and so on.
I don't know, if use the wrong keyword but I can't find a good solution for it and I'm to new on spring to figure it out by my self ...
So the problem is not to store the file on the server, that problem I solved already but till now I can only save it to the ServletContext#getRealPath() and there I think I can't access the image in the jsp site.
Did have anyone a good idea how I can solve that or any hints to find a solution ?
very thanks for your help and very sorry for my english ... it is quite late in germany -.-
According to me when you try to access the image in your jsp file it will give you 404 error.
You need to include the following line of code in your servlet.xml file.
<mvc:resources location="/assets/" mapping="/assets/**" />
You just need to replace the path you have stored your uploaded images in. And then everything will work.
Hope this helps you.
Cheers.
thx for your help, my problem is how to get this path for saving my file to it.
here is a code snippet how I do it now (I found this way in several tuts, but i do not know, if this is a good way to save images)
ServletContext context = RequestContextUtils.getWebApplicationContext(request).getServletContext();
try {
if(!image.isEmpty()) {
validateImage(image);
sizeChart.setImgPath(saveImage(context.getRealPath("/") + "/images/" + image.getOriginalFilename(), image));
}

setContentsURL related issue

Hai,
I am using Java GWT. In this i want to load one html file.
htmlPane.setContentsURL("http://www.chellasoftapp.com/ATBrowser/images/disclaimer.html");
htmlPane.setContentsURL("images/disclaimer.html");
Which one be the faster? How is it works?
In images folder of WAR I placed that html file. Is it correct place? Or Where can i upload this?
Give suggestion for this issue.
Thanks in advance.
Regards,
Sathya.A
This solution:
htmlPane.setContentsURL("http://www.chellasoftapp.com/ATBrowser/images/disclaimer.html");
has quite big disadventage. It doesn't work in development on local machine.
I would use
htmlPane.setContentsURL(GWT.getModuleBaseURL() + "/images/disclaimer.html");
This one always works, doesn't matter if your application is at http://www.chellasoftapp.com/ATBrowser/ or at URL root: http://www.chellasoftapp.com/
Can you put .html file into images directory? Yes you can, and technically it is still valid, however, it is not readable and not obvious. I.e. it is hard to understand for other developers.

Categories