0

I want to convert Android Icon to Bitmap or any other format that can be saved in the database.

Please someone assist.

Below my trying that is not working BitmapFactory.decodeResource(resources, Requires Integer here and I provided Icon that causes it is failing).

override fun onNotificationPosted(sbn: StatusBarNotification?) {
    super.onNotificationPosted(sbn)
    
    var icon : Icon? =   sbn?.notification?.getLargeIcon()
    
    var bitmap: Bitmap = BitmapFactory.decodeResource(resources,icon)
}

Any way of converting Icon to another type.

New to code Learner.

  • `getLargeIcon()` already returns a `Bitmap`. – CommonsWare Jun 26 '21 at 10:49
  • No, it does not return Bitmap – Radhe Meena Jun 26 '21 at 10:54
  • Its return public Icon getLargeIcon() { return mLargeIcon; } – Radhe Meena Jun 26 '21 at 10:56
  • What exactly is `notification`? I assumed that `notification` was a `Notification` object, and [`getLargeIcon()` on `Notification` returns a `Bitmap`](https://developer.android.com/reference/android/app/Notification). – CommonsWare Jun 26 '21 at 10:59
  • this is notification icon only but still it does not return Bitmap. I edited you will have clear picture please help if possible. – Radhe Meena Jun 26 '21 at 11:04
  • Ah, sorry, I was looking at a deprecated `largeIcon` field and getting my Kotlin syntax confused. Anyway, an `Icon` is not designed to be saved, nor converted to a `Bitmap`, so doing what you want is likely to be complicated. Off the cuff, I would [call `loadDrawable()`](https://developer.android.com/reference/android/graphics/drawable/Icon#loadDrawable(android.content.Context)) or `loadrDrawableAsync()` on it to get a `Drawable`. Then, use [something like this](https://stackoverflow.com/a/10600736/115145) to get a `Bitmap` from the `Drawable`. – CommonsWare Jun 26 '21 at 11:28

1 Answers1

0

Try with the below the code it will help you

    val icon: Icon = sbn?.notification?.getLargeIcon()
    val drawable: Drawable = icon.loadDrawable(context)
    val bitmap:Bitmap = drawable.toBitmap()
Dharmender Manral
  • 1,504
  • 1
  • 6
  • 7