Search code examples
androidiconsandroid-appwidget

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.


Solution

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