javaandroidfileandroid-package-managersandroid-applicationinfo

Application Info of a non installed app


i am trying to get some info (name and icon) from a non installed app. Basically o got the apk file, i didn't installed it yet, but i would like to display its name and icon in a list.

I am trying to do this :

public void getReceivedApps() {
    packageManager = getPackageManager();
    System.out.println("TRANSFER-RECORDActivityThe received filePath is : " + receivedFilePath);

    PackageInfo packageInfo = packageManager.getPackageArchiveInfo(receivedFilePath, PackageManager.GET_GIDS);
    System.out.println("TRANSFER RECORD ACTIVYT : O TO STRING TO PACKAGE INFO : " + packageInfo.toString());
    ApplicationInfo applicationInfo = packageInfo.applicationInfo;
    Log.d(this.getClass().getName(), "packaginf0 : " + applicationInfo.toString());
    String appName = applicationInfo.loadLabel(packageManager).toString();

    System.out.println("TRANSFER RECORD ACTIVITY  : THE APPNAME IS : " + appName);
    Drawable icon = packageManager.getPackageArchiveInfo(receivedFilePath, PackageManager.GET_GIDS).applicationInfo.loadIcon(packageManager);
    //then add it to a list.
}

The problem and it is giving me a nullpointerexception in the loadIcon and loadLabel instructions.

Btw, the receivedFilePath string has the filepath to that apk file (which is

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+getIntent().getStringExtra("receivedFilePath"); ) 

And this receivedFilePath sent as a string has the name of the file (not the app)


Solution

  • Found a similar problem, hope it helps:

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

    REF: Get apk file icon, version, name