1

I'm trying to retrieve the small icon of a Notification inside the NotificationListenerService.

I've found the notification.getSmallIcon() function that return a android.graphics.drawable.Icon. How it's possible to obtain a Bitmap from that Icon?

Francesco Re
  • 836
  • 6
  • 22

1 Answers1

4

You need to use loadDrawable or loadDrawableAsync on the icon it will return a drawable.

loadDrawable added in API level 23 public Drawable loadDrawable (Context context) Returns a Drawable that can be used to draw the image inside this Icon, constructing it if necessary. Depending on the type of image, this may not be something you want to do on the UI thread, so consider using loadDrawableAsync instead.

as shown below

Icon icon = notification.getSmallIcon(); 
Bitmap bitmap = icon.loadDrawable(context);

then you can convert the drawable to bitmap from the stackoverflow link below there are many answers how to convert drawable to Bitmap

How to convert a Drawable to a Bitmap?

tamtom
  • 2,474
  • 1
  • 20
  • 33
  • Hi, that's perfect! Also for API level lower than 23, how can I retrieve the image? – Francesco Re Feb 04 '19 at 11:34
  • @FrancescoRe the solution was based on your requirements because `getSmallIcon()` is also added on 23 API, there is a deprecated method might work `Bundle extras = getNotification().extras; extras.get(Notification.EXTRA_SMALL_ICON); ` it will retrieve an `Icon` I don't think there is a way to convert it to drawable you might rely on the icon of the package as compatibility – tamtom Feb 04 '19 at 12:36