我试图使用Notification.Builder.setLargeIcon(bitmap)
方法并传入一张位图图片。我有一张我想要使用的图片存在drawable文件夹中,那么我该如何将其转换成位图图片?
你可能想要使用Notification.Builder.setLargeIcon(Bitmap)
,对吗? :)
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
notBuilder.setLargeIcon(largeIcon);
这是一种将资源图像转换为Android Bitmap
的好方法。
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资源转换为位图的另一种方法:
Drawable drawable = getResources().getDrawable(R.drawable.input);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
。 - dkoukoul首先创建位图图像
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Notification.Builder.setLargeIcon(bmp);
val largeIcon = BitmapFactory.decodeResource(context.resources, R.drawable.my_large_icon)
res/drawable
文件夹中,
1. 创建一个新的 Drawable 资源
。
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" />
R.id.filename
。Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.your_image_id);
... E/CommitToConfigurationOperation: 快照令牌格式不正确(大小): ... E/NotificationService: 不发布具有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: 警告:在未来的版本中,这将导致应用程序崩溃:...
- Bhuro