在Android的通知中将Drawable或Bitmap设置为图标 [英] Set Drawable or Bitmap as icon In Notification in Android
问题描述
我从服务器下载一个图像作为位图,并将其转换为可绘制对象,现在我想将此可绘制对象用作通知图标.但我无法做到这一点.这是我的代码:
I download a image from server as bitmap and convert it to drawable now i want to use this drawable as notification icon. But i am unable to do that. here is my code:
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(intent)
.setSmallIcon(bitmap)
.setWhen(when)
.build();
但是图标是Resources int值,因此当我使用它时会出错.任何帮助
but icon is a Resources int value so when i used it it gives error. Any help
现在我更新我的代码,现在我正在这样做:
Now i update my code and now i am doing like that :
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(intent)
.setSmallIcon(icon)
.setLargeIcon(bitmap)
.setWhen(when)
.build();
,但是在左侧提供大图标,在右侧提供小图标.我不想要这样做,因此我删除了setSmallIcon行并运行我的代码,但未向我显示通知
but it gives large icon on left side and small icon on right side. I don't want this so for this i remove setSmallIcon line and run my code but it not showing me the notifications
推荐答案
如果您阅读特定于 setSmallIcon(int icon)
需要在可绘制应用程序的程序包中使用资源ID .
If you read the developer documents specific to Notification.Builder
you will see that setSmallIcon(int icon)
needs a A resource ID in the application's package of the drawable to use.
下载图像,转换为位图,然后将其设置为setSmallIcon()
仍然会给您带来错误.
Downloading an image, converting to a bitmap and then setting it to the setSmallIcon()
is still going to give you an error.
例如,即使您要将Bitmap
转换为Drawable
,例如:
Even if you were to convert the Bitmap
to a Drawable
like this for instance:
Drawable d = new BitmapDrawable(getResources(), bmpFinal);
它仍然会给您一个错误,因为Drawable
在您的应用程序包中不存在.
it is still going to give you an error because that Drawable
does not exist in your application package.
唯一可行的解决方案是使用package
中存在的Drawable
资源并将其设置为setSmallIcon()
方法.典型用法:
The only possible solution is to use a Drawable
resource that exists in your package
and set it to the setSmallIcon()
method. Typical usage:
builder.setSmallIcon(R.drawable.ic_launcher);
或者, setLargeIcon (Bitmap icon)
需要一个Bitmap实例.无需在当前代码中进行任何其他更改(因为您已经有一个Bitmap
),可以根据需要使用它.
Alternatively, the setLargeIcon (Bitmap icon)
requires a Bitmap instance. Without having to make any additional changes in your current code (since you already have a Bitmap
), you can use that as it is, if it fits your requirement.
如果没有,则几乎必须使用drawable
文件夹之一中已经存在的Drawable
资源.
If not, you pretty much have to use a Drawable
resource that is already present in one of the drawable
folders.
这篇关于在Android的通知中将Drawable或Bitmap设置为图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!