Access file type icons Mac OSX

2019-07-22 04:27发布

问题:

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.

回答1:

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



回答2:

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


回答3:

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