我正在尝试使用
Notification.Builder.setLargeIcon(bitmap)
来获取位图图像。我的可绘制文件夹中有要使用的图像,那么如何将其转换为位图?
您的意思可能是
Notification.Builder.setLargeIcon(Bitmap)
,对吧? :)
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
notBuilder.setLargeIcon(largeIcon);
这是将资源图像转换为 Android
Bitmap
s 的好方法。
Drawable myDrawable = getResources().getDrawable(R.drawable.logo);
Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
由于 API 22
getResources().getDrawable()
已弃用,因此我们可以使用以下解决方案。
Drawable vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.logo, getContext().getTheme());
Bitmap myLogo = ((BitmapDrawable) vectorDrawable).getBitmap();
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
Context
可以是您当前的 Activity
。
这是在android中将Drawable资源转换为Bitmap的另一种方法:
Drawable drawable = getResources().getDrawable(R.drawable.input);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
首先创建位图图像
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
现在在通知生成器图标中设置位图....
Notification.Builder.setLargeIcon(bmp);
在
res/drawable
文件夹中,
1. 创建一个新的
Drawable Resources
.
2. 输入文件名。
将在
res/drawable
文件夹内创建一个新文件。
将此代码替换到新创建的文件中,并将
ic_action_back
替换为您的可绘制文件名。
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_action_back"
android:tint="@color/color_primary_text" />
现在,您可以将其与资源 ID 一起使用,
R.id.filename
。
如果有人正在寻找大图标的 Kotlin 版本,你可以使用这个
val largeIcon = BitmapFactory.decodeResource(context.resources, R.drawable.my_large_icon)
尝试以下操作:
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.your_image_id);