30

When I add the code mNotificationBuilder.setLargeIcon(BitmapFactory.decodeResource( getResources(), R.drawable.ic_large_icon)); to my notification it stops working without errors or warnings. This only happens on pre-Lollipop, on Lollipop and beyond it works great. And with "works" I mean that the notification shows up.

My sample code:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

mBuilder.setSmallIcon(R.drawable.icon);
mBuilder.setContentTitle("Content Title");
mBuilder.setContentText("Content Text");
mBuilder.setLargeIcon(BitmapFactory.decodeResource( getResources(), R.drawable.ic_large_icon));

startForeground(1, mBuilder.build());

I have tried to load the Bitmap in different ways but it keeps failing... The icon is 128x128 so the size of it should not be a problem?

I have also tried different id's but none that solves the problem.

I would be so greatful of any advice, please any push in the right direction would mean the world to me.

EDIT 1#

This notification is issued from a Service. The service is alive and Log prints tell me that code after "startForeground()" is run.

Matti Torvaldsson
  • 311
  • 1
  • 3
  • 4
  • "The icon is 128x128 so the size of it should not be a problem?" -- what directory (or directories) do you have this icon in? – CommonsWare Mar 04 '16 at 15:19
  • Res/drawable, also tried to use the Android Asset Studio(by Roman) to make multiple resolutions to put into the different sub-directories. But it didn't help either. Is there any possibility that it matters where it is put? Going to try using the regular NotificationManager class instead of "startForeground()", but it would be weird if that was the cause of the problem. – Matti Torvaldsson Mar 04 '16 at 19:20
  • 1
    `res/drawable/` is almost never the right answer. That's a synonym for `res/drawable-mdpi/`, and so your image will be upscaled on higher-density devices. So, on really high density devices, that image that started at 128x128 might climb to 512x512, at which point you'll blow past the 1MB IPC transaction size limit. You might want to look at the `Bitmap` that you're getting back from `decodeResource()`. – CommonsWare Mar 04 '16 at 19:23
  • I'll do that, thanks! I'll get back to you when it's done. – Matti Torvaldsson Mar 04 '16 at 19:26
  • Did sadly not help. Any other ideas? – Matti Torvaldsson Mar 07 '16 at 10:24
  • mBitmap (id=831728525488) mBuffer (id=831753378024) mDensity 320 mFinalizer Bitmap$BitmapFinalizer (id=831729260528) mHeight -1 mIsMutable false mLayoutBounds null mNativeBitmap -1191370648 mNinePatchChunk null mRecycled false mWidth -1 So I guess there are something fishy in here.. but why isnt it loading the Bitmap correct? :S – Matti Torvaldsson Mar 07 '16 at 10:30

3 Answers3

32

You have to first set large icon then small icon.

in my case this code is working:

    mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_message));
    mBuilder.setSmallIcon(R.mipmap.ic_message);
    mBuilder.setContentTitle("Inbox");
    mBuilder.setContentText("New message received");
Salar Rastari
  • 2,280
  • 5
  • 22
  • 35
4

Before Lolipop there was no large icon for notifications. Small icon should be 64x64 and while creating it keep in mind it will be rendered in two colors: white and transparent.

NotificationCompat.Builder mBuilder;

if (SystemTools.isAndroidApiVersionBeforeLollipop()) {
                mBuilder =
                        new NotificationCompat.Builder(context)
                                .setContentIntent(pendingIntent)
                                .setSmallIcon(iconRid)
                                .setColor(ContextCompat.getColor(context, R.color.transparent))
                                .setContentTitle(caption)
                                .setContentText(text)
                                .setOngoing(true)
                                .setWhen(0)
                                .setPriority(NotificationCompat.PRIORITY_LOW)
                ;
            } else {
                mBuilder =
                        new NotificationCompat.Builder(context)
                                .setContentIntent(pendingIntent)
                                .setSmallIcon(iconRid)
                                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), bigIconRid))
                                .setColor(ContextCompat.getColor(context, R.color.transparent))
                                .setContentTitle(caption)
                                .setContentText(text)
                                .setOngoing(true)
                                .setWhen(0)
                                .setPriority(NotificationCompat.PRIORITY_LOW)
                ;

}

Dark
  • 864
  • 9
  • 17
4

Too, You can use:

.setLargeIcon(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon128), 128, 128, false))
  • ERROR: `java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference` – C.F.G Jul 16 '23 at 06:42