190

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?

8 Answers 8

430

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 :)
I dont think this is right as a general answer — at least not since Android started supporting vector drawables.
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:...
49
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();

5 Comments

It tells me bitmapDrawable cannot be resolved to a type
Just press ctrl+shift+O if you are receiving cannot be resolved to a type for bitmapDrawable. Cheers!
Unfortunately this way crashes my app!
getDrawable is deprecated
14
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);

Context can be your current Activity.

1 Comment

and for vector drawables?
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();

2 Comments

How is yours different from AndyW solution ? it is the same!
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
8

First Create Bitmap Image

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

now set bitmap in Notification Builder Icon....

Notification.Builder.setLargeIcon(bmp);

Comments

2

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)

Comments

1

Try the following:

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.