converting drawable resource image into bitmap

AndroidBitmap

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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();

Solution 3 - Android

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);

Context can be your current Activity.

Solution 4 - Android

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

Drawable drawable = getResources().getDrawable(R.drawable.input);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

Solution 5 - Android

First Create Bitmap Image

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

now set bitmap in Notification Builder Icon....

Notification.Builder.setLargeIcon(bmp);

Solution 6 - Android

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.

Solution 7 - Android

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontyczjView Question on Stackoverflow
Solution 1 - AndroidpoitroaeView Answer on Stackoverflow
Solution 2 - AndroidAndyWView Answer on Stackoverflow
Solution 3 - AndroidaromeroView Answer on Stackoverflow
Solution 4 - AndroidRamkailashView Answer on Stackoverflow
Solution 5 - AndroidRavi MakvanaView Answer on Stackoverflow
Solution 6 - AndroidMohammedsalim ShivaniView Answer on Stackoverflow
Solution 7 - AndroidPrateekView Answer on Stackoverflow