Set Drawable or Bitmap as icon In Notification in Android
Asked Answered
I

4

20

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:

    Notification notification = new NotificationCompat.Builder(context)

    .setContentTitle(title)

    .setContentText(message)

    .setContentIntent(intent)

    .setSmallIcon(bitmap)

    .setWhen(when)

    .build(); 

but icon is a Resources int value so when i used it it gives error. Any help

Edit:

Now i update my code and now i am doing like that :

          Notification notification = new NotificationCompat.Builder(context)

        .setContentTitle(title)

        .setContentText(message)

        .setContentIntent(intent)

        .setSmallIcon(icon)

        .setLargeIcon(bitmap)

        .setWhen(when)

        .build();

but it gives large icon on left side and small icon on right side. I don't want this so for this i remove setSmallIcon line and run my code but it not showing me the notifications

Icarian answered 17/4, 2013 at 8:22 Comment(3)
https://mcmap.net/q/661743/-create-a-notification-with-saved-icon ... se my answer hereEgon
lol for some reason i dont know why .. the small icon at the right side is called large icon .. only there you can set the bitmap. In higher api you can create your own notification with custom layoutEgon
did you tried with this ? Notification notification = new Notification(R.drawable.logo,"App ", System.currentTimeMillis());Pneumatic
A
28

If you read the developer documents specific to Notification.Builder you will see that setSmallIcon(int icon) needs a A resource ID in the application's package of the drawable to use.

Downloading an image, converting to a bitmap and then setting it to the setSmallIcon() is still going to give you an error.

Even if you were to convert the Bitmap to a Drawable like this for instance:

Drawable d = new BitmapDrawable(getResources(), bmpFinal);

it is still going to give you an error because that Drawable does not exist in your application package.

The only possible solution is to use a Drawable resource that exists in your package and set it to the setSmallIcon() method. Typical usage:

builder.setSmallIcon(R.drawable.ic_launcher);

Alternatively, the setLargeIcon (Bitmap icon) requires a Bitmap instance. Without having to make any additional changes in your current code (since you already have a Bitmap), you can use that as it is, if it fits your requirement.

If not, you pretty much have to use a Drawable resource that is already present in one of the drawable folders.

Androus answered 17/4, 2013 at 8:38 Comment(0)
T
21

you can try using this method

 builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));

http://javatechig.com/android/android-notification-example-using-notificationcompat

Tris answered 27/9, 2015 at 23:3 Comment(1)
This isn't answering what OP asked.Downtrend
H
15

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);
Hegelianism answered 29/7, 2016 at 10:40 Comment(1)
Thanks for the last one, even though there's still no solution for old version.. Good to know anywaysStylize
L
6

Better option get application icon

 Drawable drawable=getApplicationInfo().loadIcon(getPackageManager());
 Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();



.setSmallIcon(getApplicationInfo().icon)
.setLargeIcon(bitmap)
Lorgnon answered 27/7, 2017 at 6:23 Comment(1)
Excellent option, but careful this makes the app crashes if the icon is an adaptative icon, as android toolbar doesn't support them.Magruder

© 2022 - 2024 — McMap. All rights reserved.