SubName: GetFileIcon
Description: Get the FileIcon from an exe for ex.
Dependencies: JavaObject
Tags: GetFileIcon, FileIcon
Example
Description: Get the FileIcon from an exe for ex.
Dependencies: JavaObject
Tags: GetFileIcon, FileIcon
B4X:
Sub GetFileIcon(Dir As String, FileName As String) As Image
Dim jo As JavaObject
Dim JavaFile As JavaObject
Dim icon As JavaObject
JavaFile.InitializeNewInstance("java.io.File", Array(File.Combine(Dir, FileName)))
If GetSystemProperty("os.name", "").ToLowerCase.Contains("win") Then
icon = jo.InitializeStatic("sun.awt.shell.ShellFolder").RunMethodJO("getShellFolder", Array(JavaFile)).RunMethod("getIcon", Array(True))
Else
icon = jo.InitializeStatic("sun.awt.shell.ShellFolder").RunMethodJO( _
"getShellFolder", Array(JavaFile)).RunMethod("getIcon", Array(True))
Dim fc As JavaObject
fc.InitializeNewInstance("javax.swing.JFileChooser", Null)
Dim icon As JavaObject = fc.RunMethodJO("getUI", Null).RunMethodJO("getFileView", Array(fc)).RunMethod("getIcon", Array(JavaFile))
Dim AquaIcon As JavaObject
AquaIcon.InitializeStatic("com.apple.laf.AquaIcon")
icon = AquaIcon.RunMethod("getImageForIcon", Array(icon))
End If
Return AwtImageToImage(icon)
End Sub
Private Sub AwtImageToImage(img As JavaObject) As Image
Log(GetType(img))
Dim jo As JavaObject
Return jo.InitializeStatic("javafx.embed.swing.SwingFXUtils").RunMethod("toFXImage", Array(img, Null))
End Sub
Example
B4X:
ImageView1.SetImage(GetFileIcon("C:\Program Files (x86)\Anywhere Software\B4J", "B4J.exe"))
Last edited by a moderator: