Nitesh Garg
Nitesh Garg

Reputation: 23

Get other applications icon using uri

I have an appwidget and I want to show other installed applications' launcher icon on it. I am able to get the icons using either of the following two codes

pkgInfo.applicationInfo.loadIcon(context.getPackageManager());

OR

context.getPackageManager().getApplicationIcon(pkgInfo.packageName);

where pkgInfo is the instance of PackageInfo

Everything works fine except for a couple of apps, which do not show their icons and there is no error printed in the logs. According to an answer here we can also use URIs to get the drawables.

My question is if android.resource://[package]/[res type]/[res name] is the URI then how to get the [res name]? And how to get the drawable of OTHER application icons from this URI?

Thanks in advance.

Upvotes: 2

Views: 1763

Answers (2)

Sven
Sven

Reputation: 879

In case you don't know the resource name, you can also access resources by their id:

android.resource://[package]/[res_id]

The resource id of the app icon is available in the ApplicationInfo of the app:

ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
if(appInfo.icon != 0) {
    Uri uri = Uri.parse("android.resource://" + packageName + "/" + appInfo.icon);
}

Upvotes: 2

Nitesh Garg
Nitesh Garg

Reputation: 411

It looks like the problem was while converting Drawable to Bitmap. The conversion was required as RemoteViews do not have a method setImageDrawable that can be used with an ImageView. Making the below change fixed the problem.

Before

Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

After

Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap); 
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

Upvotes: 0

Related Questions