Android Question Get App Icon from APK

Fusseldieb

Active Member
Licensed User
Longtime User
Hi,
I've found a little snippet on how to get the App Icon from an APK.
Is it possible to get that working in B4A, maybe with reflection?

B4X:
 String APKFilePath = "mnt/sdcard/myapkfile.apk"; //For example...
PackageManager pm = getPackageManager();
PackageInfo    pi = pm.getPackageArchiveInfo(APKFilePath, 0);

// the secret are these two lines....
pi.applicationInfo.sourceDir       = APKFilePath;
pi.applicationInfo.publicSourceDir = APKFilePath;
//

Drawable APKicon = pi.applicationInfo.loadIcon(pm);
String   AppName = (String)pi.applicationInfo.loadLabel(pm);

It would be nice, if anyone could help me out...
 

udg

Expert
Licensed User
Longtime User
Did you read this thread?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi,

from the thread you suggested, combining @agraham and @JakeBullet70 's code, you have:
B4X:
  Dim args(1) As Object
  Dim Obj1, Obj2, Obj3, Obj4 As Reflector
  Dim size, i, flags, count As Int
  Dim Types(1), msg, name As String
  Obj1.Target = Obj1.GetContext
  Obj1.Target = Obj1.RunMethod("getPackageManager") ' PackageManager
  Obj2.Target = Obj1.RunMethod2("getInstalledPackages", 0, "java.lang.int") ' List<PackageInfo>
  size = Obj2.RunMethod("size")
  For i = 0 To size -1
    Obj3.Target = Obj2.RunMethod2("get", i, "java.lang.int") ' PackageInfo
    name = Obj3.GetField("packageName")
    Obj3.Target = Obj3.GetField("applicationInfo") ' ApplicationInfo 
    flags = Obj3.GetField("flags")
    Dim icon As BitmapDrawable '  *** NEW LINE  ***
    If Bit.AND(flags, 1)  = 0 Then
      'app is not in the system image
      args(0) = Obj3.Target
      Types(0) = "android.content.pm.ApplicationInfo"
      name = Obj1.RunMethod4("getApplicationLabel", args, Types)
      icon = Obj1.RunMethod4("getApplicationIcon", args, Types)  '  *** NEW LINE  ***
      msg = msg & name & " : " & flags & CRLF
      count = count + 1 
    End If
  Next 
  Msgbox(msg,count)
  Return

Or you could use the approach suggested by @DonManfred in that same thread:
B4X:
  Activity.LoadLayout("Layout1")
  lv.Clear
  Dim pm As PackageManager
  Dim packages As List
  packages = pm.GetInstalledPackages
  For i = 0 To packages.Size - 1
    Dim p As String = packages.Get(i)
    Dim bdw As BitmapDrawable = pm.GetApplicationIcon(p)
    lv.AddTwoLinesAndBitmap(pm.GetApplicationLabel(p),packages.Get(i),bdw.Bitmap)
    Log(packages.Get(i))
  Next

I didn't test either of the above approaches myself but they should both work.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Two notes.
B4X:
If Bit.AND(flags, 1) = 0 Then
'in your case could be
If Bit.AND(flags, 262144) = 262144 Then
'to test for apps installed on  external/removable/unprotected storage
For flags definitions have a look here.
To look for TestApp.apk compare "name" from code row:
B4X:
name = Obj3.GetField("packageName") 'earlier in code so you have to save it, since it gets reused for app's label later in code
with it and take proper action.

Hope the above could be helpful.
 
Last edited:
Upvote 0
Top