Skip to content Skip to sidebar Skip to footer

Retrieving Xhdpi App Icons From Packagemanager?

I have an activity in my app wherein a ListView of all installed apps is generated. In addition to the app name, the app icon appears as well. I had created an array of objects con

Solution 1:

ApplicationInfoapplicationInfo= 
        packagemanager.getApplicationInfo(packageName(), PackageManager.GET_META_DATA);
Resourcesres= packagemanager.getResourcesForApplication(applicationInfo);
DrawableappIcon= res.getDrawableForDensity(applicationInfo.icon, 
                                             DisplayMetrics.DENSITY_XXXHIGH, 
                                             null);

Solution 2:

I suspect there is some other reason why the icons look bad (such as resizing within your app), but if you need to do it for any Android version:

// Get the application's resourcesResourcesres= mPackageManager.getResourcesForApplication(appInfo);

// Get a copy of the configuration, and set it to the desired resolutionConfigurationconfig= res.getConfiguration();
ConfigurationoriginalConfig=newConfiguration(config);
config.densityDpi = DisplayMetrics.DENSITY_XHIGH;

// Update the configuration with the desired resolutionDisplayMetricsdm= res.getDisplayMetrics();
res.updateConfiguration(config, dm);

// Grab the app iconDrawableappIcon= res.getDrawable(appInfo.icon);

// Set our configuration back to what it was
res.updateConfiguration(originalConfig, dm);

Solution 3:

Here's a new answer to an old question, just to put everything in one place. You'll need to get the devices default density, and resize the image to ensure that you have a properly conforming size, as just requesting the icon does not always return proper size.

Consider these two methods, one to obtain device size, and the second to resize your drawable:

privateintgetDeviceDpi(){
        DisplayMetricsdm=newDisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.densityDpi;
}

private Drawable getSizedAppIcon(Context context, String packageName, int Density)throws PackageManager.NameNotFoundException {
    //for @param Density you can use a static from DisplayMetrics.<something>PackageManagerpm= Objects.requireNonNull(context).getPackageManager();
        ApplicationInfoappInfo=this.getPackageManager().getApplicationInfo(packageName, 0);
        Drawableicon= pm.getApplicationIcon(appInfo);
        Bitmapbitmap= ((BitmapDrawable) icon).getBitmap();
        switch (Density){
            case DisplayMetrics.DENSITY_LOW: //120returnnewBitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap,
                        32, 32, true));
            case DisplayMetrics.DENSITY_MEDIUM: //160returnnewBitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap,
                        48, 48, true));
            case DisplayMetrics.DENSITY_HIGH: //240returnnewBitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap,
                        72, 72, true));
            case DisplayMetrics.DENSITY_XHIGH: //320returnnewBitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap,
                        96, 96, true));
            case DisplayMetrics.DENSITY_XXHIGH: //480returnnewBitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap,
                        144, 144, true));
            case DisplayMetrics.DENSITY_XXXHIGH: //640returnnewBitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap,
                        192, 192, true));
            default:
                return icon;
        }

}

Then call them like in this example:

try{
    getSizedAppIcon(this, "com.example.app",getDeviceDpi());
}catch (Exception ignore) {}

Post a Comment for "Retrieving Xhdpi App Icons From Packagemanager?"