Access file type icons Mac OSX - java

I am trying to find a way to access the Mac OSX system icons. Does anyone know their default location on a Mac? Or know of a way using Java to have them returned?
There is a method of using JFileChooser for Mac to retrieve an Icon for a file, but the file has to exist on the file system (in my app the file could be streaming from a server so short of creating a dummy file on the system with that extension it wont work).
I can access them on Windows in the following way using SWT (but this bombs on a Mac). The variable "fileType" below for example is ".txt", ".bmp", ".doc", etc:
Program p;
Image image;
//find the correct OS image for the file type and set
//the image to the tree item
p = Program.findProgram(fileType);
ImageData data = p.getImageData();
image = new Image(display, data);
UPDATE: There does not appear to be a clear way to import these. I ended up finding some generic Mac icons online and bundling them with my app to simply use getRecourceAsStream() when on a Mac until a better solution is found.

It is late, but maybe somebody else would search for the same problem (like me).
The FileSystemView trick works only for 16x16 images on all platforms. On Mac, you need to use the default Aqua look and feel to make it work.
For Windows, you can use ShellFolder.getShellFolder(file).getIcon(true) to get a 32x32 icon.
For Mac, you can use Quaqua that comes with some Objective-C jni libs that give you the desired/available icon size for any file (16px, 32, 64, 128, 256, 512) :
http://www.randelshofer.ch/quaqua/javadoc/ch/randelshofer/quaqua/osx/OSXFile.html#getIcon%28java.io.File,%20int%29

On OS X, a FileView works much better than a FileSystemView. I'm using the following to get icons for files:
final JFileChooser fc = new JFileChooser();
//return fc.getFileView().getIcon(f); // will throw a null pointer
Icon result = fc.getUI().getFileView(fc).getIcon(f);

I think the FileSystemView and its friends provide way for getting file icons.

Related

Getting image path to use it as system tray icon

So I've been looking at many, many solutions to this "problem" but somehow nothing worked for me. I'm trying to set a system tray icon for my Java application, but no matter what I do, the icon just won't show up. Instead there's a blank spaceholder. I've tried different things to get the image path but it seems like I can't get the correct image path.
This is what the code currently looks like:
URL url = System.class.getResource("image.jpg");
Image img = Toolkit.getDefaultToolkit().getImage(url);
SystemTray mainTray = SystemTray.getSystemTray();
TrayIcon trayIconImage = new TrayIcon(img, "tray icon");
mainTray.add(trayIconImage);
System.print.out(url) returns null
What am I doing wrong when getting the path?
I can post a screenshot of the Eclipse folders in case that helps.
You can drag and drop the image into the package folder where your .java files are shown inside the IDE. Just drag the file from the desktop, or whatever location, and drop it into the IDE where you see your source files. after that you can simply refer to the image, "image.jpg", in your code when declaring a File or Image, or whatever class you use to make it into an object. When you export the project the image will be included so you will not have to worry about the path. Should be similar to what I did in the photo. I dragged and dropped the int.txt file into the source package.
if this was helpful accept the answer.
You should be able to use this:
Image img = new BufferedImage("image.jpg");
SystemTray mainTray = SystemTray.getSystemTray();
TrayIcon trayIconImage = new TrayIcon(img, "tray icon");
mainTray.add(trayIconImage);
Basically the program can find the file because they are in the same directory which is the source packages folder.
You may want to try using the Path class and File class if you are worried about getting paths.

How to set up Eclipse project to load an image so that it also runs from the command line

I've built a Java application that loads an image at runtime. The location of the image is fixed relative to the project.
I would like to be able to run the program from both within Eclipse and the command line and for it to load the image correctly. However, I can only do one or the other but not both. This seems like such a trivial thing to want to do but I can't find out how to do it.
The project is set up so that it creates a bin directory for the output and puts the image in a resources sub-folder. This is fine when running from the command line as I can write my code to look in that sub folder for the file.
But when I run the program from within eclipse the current working directory is different.
What am I missing?
TIA
Update - adding some code
This is what I had originally:
BufferedImage awtImage = ImageIO.read(new File(System.getProperty("user.dir") + "/resources/image-name.png"));
Following the advice in the comments I am trying to use getResourceAsStream but I don't know what to pass to the File constructor.
InputStream temp = MyClass.class.getResourceAsStream("resources/image-name.png");
BufferedImage awtImage = ImageIO.read(new File(???));
The resource is being found because temp is not null.
I think there's 2 solutions.
1) you specify an absolute path
2) your image is in the classpath so you could load it via :
YouClass.class.getResourceAsStream("YourImg.png");
The working directory, if that's really what you mean, is not a great place to load an image from. It appears that you have an image that you would distribute with your finished program so that the program could use it. In that case, I suggest that you use Class.getResourceAsStream(), and put the image in the directory with (or near) that class.
EDIT:
Here is code I used in one of my programs for a similar purpose:
ImageIcon expandedIcon = null;
// ...
expandedIcon = new ImageIcon(TreeIcon.class.getResource("images/Expanded.png"));
The ImageIcon class is part of Swing; I don't know if you're using that, but this should serve to show you the idea. The getResource() method takes a URL; again, you might need something a little different. But this shows the pathname relative to the path of the class on which the method is called, so if TreeIcon is in x/y/z/icons, the PNG file needs to be in x/y/z/icons/images, wherever that is on that computer.
TreeIcon is a class of mine, and its internals will not help you, so I'm not posting them. All it's doing here is providing a location for the PNG file I'm loading into an ImageIcon instance.
In addition to working on a disk with a directory structure, this also works in a jar file (which is a common way to distribute a java program or library). The jar file is just a zip file, and each file in the jar/zip file has its directory associated with it, so the image can be in the jar in the correct directory just as the java classes are in their directories.
getResourceAsStream() returns a stream; if you want to use that byte stream to load as an image, find a class that converts an stream to something your image class can use as a constructor or in a load method and hook them up. This is a common thing to have to figure out with Java i/o, unfortunately there is no cookbook way to do it across all images and situations, so we can't just tell you what it is.
EDIT 2:
As from the comment, try:
ImageIO.read(new File(MyClass.class.getResource("resources/image-name.png");
I set up my Eclipse projects like this.
The input directory is added to the classpath (JavaBuildPath in Eclipse).
Finally, you access the image and / or text files like this.
private BufferedImage getIconImage() {
try {
return ImageIO.read(getClass().getResourceAsStream(
"/StockMarket.png"));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

FileSystemView get war file icon null pointer

I am using javax.swing.chooser.FileSystemView to get system file icons and display them for uploaded files in my web application.
It is all working well for various file types, however when I am trying to upload a .war file, Java is returning a Null Pointer Exception. How can I know if this particular file type is supported or not by this method?
Anyone had a similar issue?
This is the code I am using
// gets a 16x16 size image icon for list view
Icon smallIcon = FileSystemView.getFileSystemView().getSystemIcon(file);
Image smallImage = ((ImageIcon) smallIcon).getImage();
I am running this code on a Windows 7 machine if it makes any difference.
Thanks for your help :)
Edit: Turns out this problem wasn't the icon. I have this line of code
String contentType = getServletContext().getMimeType(file.getName());
which accepts the file I uploaded as a parameter and checks the type of file. I am doing this to check if the file is an image or not. For the case of a war file it is returning null for some reason

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());

What is the correct path for Toolkit.getImage()?

I need to upload an image file and generate a thumbnail for the uploaded file in my JSF webapplication. The original image is stored on the server in /home/myname/tomcat/webapps/uploads, while the thumbnail is stored in /home/myname/tomcat/webapps/uploads/thumbs. I'm using the thumbnail generator class I copied from philreeve.com.
I have successfully uploaded the file with help from BalusC. But using Toolkit.getImage(), I can't access the image.
I used the uploaded file's absolute path, like so:
inFilename = file.getAbsolutePath();
The relevant code from the thumbnail generator is:
public static String createThumbnail(String inFilename, String outFilename, int largestDimension) {
...
Image inImage = Toolkit.getDefaultToolkit().getImage(inFilename);
if (inImage.getWidth(null) == -1 || inImage.getHeight(null) == -1) {
return "Error loading file: \"" + new File(inFilename).getAbsolutePath() + "\"";
}
...
}
Since I am already using the absolute path, I don't understand why it is not working. I have also used the following values for inFilename, but I always get the "Error loading file...".
/home/myname/tomcat/webapps/uploads/filename.ext
/uploads/filename.ext
But I did check the directory, and the image is there. (I uploaded using /home/myname/tomcat/webapps/uploads/filename.ext, and it works.) What is the correct path for the image in that directory? Thank you.
Update
I got the code to work by using:
Image inImage = ImageIO.read(new File(inFilename));
I still don't understand why Toolkit.getImage() does not work though.
Are you sure it's a JPEG file? Use an image viewer to make sure nothing bad happened to the file during upload (or that it was an image to begin with).
Also, use new File(inFilename).exists() to make sure the path is correct. I also suggest to print new File(inFilename).getAbsolutePath() in error messages because relative paths can hurt you.
That said, the rest of the code looks correct.
The problem is that Toolkit.getImage() does not return the image immediately. The issue is well-described in this bug report, a relevant extract of which is here:
This is not a bug. The submitter is not properly using the asynchronous
Image API correctly. He assumes that getImage loads all of the image's bits
into memory. However, it is well documented that the actual loading of
bits does not take place until a call to Component.prepareImage or
Graphics.drawImage. In addition, these two functions return before the
Image is fully loaded. Developers are required to install an ImageObserver
to listen for notification that the Image has been fully loaded. Once they
receive this notification, they can repaint the Image.
I found that the answer to this question works well:
Image image = new ImageIcon(this.getClass().getResource("/images/bell-icon16.png")).getImage();

Categories