187

I am trying to use the Notification.Builder.setLargeIcon(bitmap) that takes a bitmap image. I have the image I want to use in my drawable folder so how do I convert that to bitmap?

Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
tyczj
  • 71,600
  • 54
  • 194
  • 296

8 Answers8

427

You probably mean Notification.Builder.setLargeIcon(Bitmap), right? :)

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
notBuilder.setLargeIcon(largeIcon);

This is a great method of converting resource images into Android Bitmaps.

poitroae
  • 21,129
  • 10
  • 63
  • 81
  • 2
    Why not hit the "Edit" button and fix the question? (More a suggestion for the future - I already did it for this one... I'd suggest editing your answer to not criticize their typos. I'm not doing it for you.) On another note, +1 for having a working answer :) – ArtOfWarfare Oct 18 '12 at 18:19
  • 1
    I dont think this is right _as a general answer_ — at least not since Android started supporting vector drawables. – roberto tomás Mar 24 '16 at 12:52
  • after implementing the solution I'm getting this ... `... E/CommitToConfigurationOperation: Malformed snapshot token (size): ... E/NotificationService: Not posting notification with icon==0: Notification(pri=0 contentView=null vibrate=null sound=content://settings/system/notification_sound defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE) ... E/NotificationService: WARNING: In a future release this will crash the app:...` – Bhuro Sep 26 '16 at 05:31
48
Drawable myDrawable = getResources().getDrawable(R.drawable.logo);
Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();

Since API 22 getResources().getDrawable() is deprecated, so we can use following solution.

Drawable vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.logo,  getContext().getTheme());
Bitmap myLogo = ((BitmapDrawable) vectorDrawable).getBitmap();
AndyW
  • 1,431
  • 16
  • 22
13
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);

Context can be your current Activity.

aromero
  • 25,681
  • 6
  • 57
  • 79
9

Here is another way to convert Drawable resource into Bitmap in android:

Drawable drawable = getResources().getDrawable(R.drawable.input);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
DroidDev
  • 1,527
  • 4
  • 20
  • 42
Ramkailash
  • 1,852
  • 1
  • 23
  • 19
  • 2
    How is yours different from AndyW solution ? it is the same! – Fahad Alduraibi Feb 16 '16 at 21:54
  • I tried this and it produces an error that it can not be cast: ```java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable``` – dkoukoul Feb 20 '23 at 09:40
8

First Create Bitmap Image

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);

now set bitmap in Notification Builder Icon....

Notification.Builder.setLargeIcon(bmp);
Ravi Makvana
  • 2,872
  • 2
  • 24
  • 38
1

If someone is looking for the Kotlin version for the large icon, you may use this

val largeIcon = BitmapFactory.decodeResource(context.resources, R.drawable.my_large_icon)
Prateek
  • 502
  • 5
  • 16
0

In res/drawable folder,

1. Create a new Drawable Resources.

2. Input file name.

A new file will be created inside the res/drawable folder.

Replace this code inside the newly created file and replace ic_action_back with your drawable file name.

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_action_back"
    android:tint="@color/color_primary_text" />

Now, you can use it with Resource ID, R.id.filename.

Mohammedsalim Shivani
  • 1,793
  • 3
  • 19
  • 30
0

Try the following:

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.your_image_id);