How to show icon of .apk File without installing?

salanmar

Member
Licensed User
Longtime User
Hi All.
How I can do this with the reflection library?

B4X:
    if (file.getPath().endsWith(".apk")) {
        String filePath = file.getPath();
        PackageInfo packageInfo = context.getPackageManager().getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
        if(packageInfo != null) {
            ApplicationInfo appInfo = packageInfo.applicationInfo;
            if (Build.VERSION.SDK_INT >= 8) {
                appInfo.sourceDir = filePath;
                appInfo.publicSourceDir = filePath;
            }
            Drawable icon = appInfo.loadIcon(context.getPackageManager());
            bmpIcon = ((BitmapDrawable) icon).getBitmap();
        }
    }
 

joseluis

Active Member
Licensed User
Longtime User
This example shows the icon for the current program. I know that what you want is to grab the icon from an uninstalled apk, but it maybe helps you something.

B4X:
Sub Process_Globals
End Sub
Sub Globals
   Dim img As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   img.Initialize("")
   getResourceBitmap(img, "icon")
   Activity.AddView(img, 0,0,72,72)
End Sub

Sub getResourceBitmap(Control As View, ImageName As String)
   Dim r As Reflector
   Dim package As String
   Dim id As Int
   package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
   id = r.GetStaticField(package & ".R$drawable", ImageName)
   r.Target = r.GetContext
   r.Target = r.RunMethod("getResources")
   Control.Background = r.RunMethod2("getDrawable", id, "java.lang.int")
End Sub

Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You can also get the app icon by using PackageManager, no need to use the Reflection library but the Phone library.

The code below displays the YouTube icon on a message box:
B4X:
Dim pm As PackageManager
Dim Icons As BitmapDrawable

Icons = pm.GetApplicationIcon("com.google.android.youtube")

Msgbox2("You Tube", "You Tube Icon", "Close", "", "", Icons.Bitmap)
 
Upvote 0

salanmar

Member
Licensed User
Longtime User
Thanks for answering. I've been able to do the code.

This is the code that works

B4X:
   Dim icon As BitmapDrawable
   Dim r, r2 As Reflector
   Dim packageName, fullpath As String
   fullpath="/mnt/sdcard/AppName.apk"
   
   r.Target = r.GetContext
   r.Target = r.RunMethod("getPackageManager") ' PackageManager
   r2.Target = r.RunMethod3("getPackageArchiveInfo", fullpath , "java.lang.String", 1, "java.lang.int") ' List<PackageInfo>
   packageName=r2.GetField("packageName")
   
   r2.Target=r2.GetField("applicationInfo")
   r2.SetField("publicSourceDir",fullpath , "java.lang.String")
   r2.SetField("sourceDir",fullpath , "java.lang.String")
   icon=r.RunMethod4("getApplicationIcon",   Array As Object(r2.Target), _
    Array As String("android.content.pm.ApplicationInfo"))   
   Msgbox2(packageName, "packageIcon", "Close", "", "", icon.Bitmap)
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
How to determine if the app has icon ? I mean not the default green Droid.
 
Upvote 0
Top