在Android的设置可绘制或位图作为图标在Notificatio [英] Set Drawable or Bitmap as icon In Notificatio in Android

查看:252
本文介绍了在Android的设置可绘制或位图作为图标在Notificatio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器作为位图下载一个图像转换现在我想用这个作为绘制的通知图标来绘制的。但我无法做到这一点。这里是我的code:

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

但图标是一个资源int值,所以当我用它提示错误。任何帮助

but icon is a Resources int value so when i used it it gives error. Any help

编辑:

现在更新我的code,现在我做这样的:

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线运行我的code,但它不显示我的通知

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

推荐答案

如果你看过具体到<一个开发文档href="http://developer.android.com/reference/android/app/Notification.Builder.html"><$c$c>Notification.Builder你会看到,<一个href="http://developer.android.com/reference/android/app/Notification.Builder.html#setSmallIcon"><$c$c>setSmallIcon(int图标) 需要的在绘制的应用程序包的资源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.

即使你的位图转换为绘制对象这样的,例如:

Even if you were to convert the Bitmap to a Drawable like this for instance:

Drawable d = new BitmapDrawable(getResources(), bmpFinal);

但还是想给你一个错误,因为绘制对象确实不存在在你的应用程序包。

it is still going to give you an error because that Drawable does not exist in your application package.

唯一可能的解决办法是使用绘制对象资源存在于你的键,将其设置为 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);

另外,在<一个href="http://developer.android.com/reference/android/app/Notification.Builder.html#setLargeIcon"><$c$c>setLargeIcon (位图图标) 需要一个位图实例。而无需进行在当前code任何其他变化(因为你已经有一个位图),您可以使用,因为它是,如果它符合您的要求。

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.

如果没有,则pretty的多少必须使用绘制对象的资源,已经是present中的绘制一个文件夹。

If not, you pretty much have to use a Drawable resource that is already present in one of the drawable folders.

这篇关于在Android的设置可绘制或位图作为图标在Notificatio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆