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?

- 6,200
- 6
- 45
- 64

- 71,600
- 54
- 194
- 296
8 Answers
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 Bitmap
s.

- 21,129
- 10
- 63
- 81
-
2Why 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
-
1I 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
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();

- 1,431
- 16
- 22
-
1
-
Hi @20Cents did you tried http://stackoverflow.com/questions/18218938/why-does-it-say-bitmap-cannot-be-resolved-to-a-type-in-this-case – AndyW Nov 26 '14 at 19:40
-
Just press ctrl+shift+O if you are receiving cannot be resolved to a type for bitmapDrawable. Cheers! – portfoliobuilder Mar 05 '15 at 00:38
-
-
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
Context
can be your current Activity
.

- 25,681
- 6
- 57
- 79
Here is another way to convert Drawable resource into Bitmap in android:
Drawable drawable = getResources().getDrawable(R.drawable.input);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

- 1,527
- 4
- 20
- 42

- 1,852
- 1
- 23
- 19
-
2
-
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
First Create Bitmap Image
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
now set bitmap in Notification Builder Icon....
Notification.Builder.setLargeIcon(bmp);

- 2,872
- 2
- 24
- 38
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)

- 502
- 5
- 16
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
.

- 1,793
- 3
- 19
- 30
Try the following:
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.your_image_id);

- 1
- 2