B4J Code Snippet Get the FileIcon from a file

SubName: GetFileIcon

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:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Erel stop liking your own code
You are missing the important point. You don't 'like' the post content. You 'like' the author that made the post. This is the way to say thank you to DonManfred who took the time to make this post and help others. :)
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Which OSX version icon pixel standard is this from? I've done some research and found that there's different icon sizes for various OSX versions. The one provided here displays a low quality icon compared to the standard we can expect on todays desktop applications. Follow this link to see the pixel standards available for Mac here and update the code accordingly. Cheers
 

DonManfred

Expert
Licensed User
Longtime User
You should ALWAYS create a NEW THREAD instead of posting to existing threads.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
@DonManfred You seem so convinced even though it concerns an update to the same question? does not the icon size standard seem relevant to this thread?
 
Last edited:
Top