185

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,092
  • 6
  • 40
  • 63
tyczj
  • 69,495
  • 52
  • 182
  • 280

7 Answers7

425

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
  • 20,729
  • 9
  • 59
  • 80
  • 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
47
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,401
  • 15
  • 22
13
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);

Context can be your current Activity.

aromero
  • 25,403
  • 6
  • 54
  • 78
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,519
  • 4
  • 20
  • 42
Ramkailash
  • 1,842
  • 1
  • 22
  • 19
7

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,899
  • 2
  • 23
  • 36
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,693
  • 3
  • 18
  • 29
0

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
  • 407
  • 5
  • 13