I download a image from server as bitmap and convert it to drawable now i want to use this drawable as notification icon. But i am unable to do that. here is my code:
<
There is some points about this question, mainly related with API 23+, if you are only interested in setSmallIcon, go to the 2nd and 3rd topics.
1st :
You can set the LargeIcon from a Drawable (instead of Resource id), like the following
Drawable drawable= ContextCompat.getDrawable(this,R.drawable.your_drawable);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setLargeIcon(bitmap)
.setContentTitle("hahah")
.setContentText("Tap to stop")
.setOngoing(true);
2nd :
If you need to set a small icon in API below 23, you will need to set a resource id like R.drawable.your_resource
.
The NotificationCompat.Builder
does not allow you to use Drawables or Bitmaps in setSmallIcon()
.
3rd :
fortunately , the support has been expanded to Icon
type on setSmallIcon()
in version 23+, using the Notification.Builder, like following :
Drawable drawable = ContextCompat.getDrawable(this,R.drawable.your_drawable);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
Notification.Builder mBuilder =
new Notification.Builder(context)
.setSmallIcon(Icon.createWithBitmap(bitmap))
.setLargeIcon(bitmap)
.setContentTitle("hahah")
.setContentText("Tap to stop")
.setOngoing(true);