【发布时间】:2012-02-01 19:00:06
【问题描述】:
我正在尝试使用获取位图图像的Notification.Builder.setLargeIcon(bitmap)。我的可绘制文件夹中有我想要使用的图像,那么如何将其转换为位图?
【问题讨论】:
我正在尝试使用获取位图图像的Notification.Builder.setLargeIcon(bitmap)。我的可绘制文件夹中有我想要使用的图像,那么如何将其转换为位图?
【问题讨论】:
您可能是指Notification.Builder.setLargeIcon(Bitmap),对吧? :)
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
notBuilder.setLargeIcon(largeIcon);
这是将资源图像转换为 Android Bitmaps 的好方法。
【讨论】:
... 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:...
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 一起使用。
【讨论】: