1

I'm trying to get an icon of the installed app. Using the following way, I'm getting the error:

The method getContext() is undefined for the type MainActivity

try{
   String pkg = "com.app.my";//your package name
   Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg);
   imageView.setImageDrawable(icon);
}
catch (PackageManager.NameNotFoundException ne) {

}

It would be very helpful if someone could guide me. Thanks in advance!!

marksyzm
  • 5,281
  • 2
  • 29
  • 27
varunkr
  • 5,364
  • 11
  • 50
  • 99

2 Answers2

2

Delete getContext(). Activity extends Context. Just call getPackageManager().getApplicationIcon(pkg).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Activity class extends Context .So just call getContext(); This will make use of the activities layout defined context.

Koen Demonie
  • 539
  • 1
  • 7
  • 24