Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中将可绘制或位图设置为通知中的图标_Android_Android Notifications_Android Notification Bar - Fatal编程技术网

在Android中将可绘制或位图设置为通知中的图标

在Android中将可绘制或位图设置为通知中的图标,android,android-notifications,android-notification-bar,Android,Android Notifications,Android Notification Bar,我从服务器下载了一个位图图像,并将其转换为可绘制图像。现在,我想使用此可绘制图像作为通知图标。但我无法做到这一点。这是我的密码: Notification notification = new NotificationCompat.Builder(context) .setContentTitle(title) .setContentText(message) .setContentIntent(intent) .setSmallIcon(bitm

我从服务器下载了一个位图图像,并将其转换为可绘制图像。现在,我想使用此可绘制图像作为通知图标。但我无法做到这一点。这是我的密码:

    Notification notification = new NotificationCompat.Builder(context)

    .setContentTitle(title)

    .setContentText(message)

    .setContentIntent(intent)

    .setSmallIcon(bitmap)

    .setWhen(when)

    .build(); 
但icon是一个Resources int值,所以当我使用它时,它会给出错误。有什么帮助吗

编辑:

现在我更新我的代码,现在我这样做:

          Notification notification = new NotificationCompat.Builder(context)

        .setContentTitle(title)

        .setContentText(message)

        .setContentIntent(intent)

        .setSmallIcon(icon)

        .setLargeIcon(bitmap)

        .setWhen(when)

        .build();

但它在左侧给出了大图标,在右侧给出了小图标。我不想这样,因此为此,我删除了setSmallIcon行并运行代码,但它没有向我显示通知

如果您阅读特定于您的开发人员文档,您将看到在可使用的应用程序包中需要一个资源ID

下载图像,将其转换为位图,然后将其设置为
setSmallIcon()
,仍然会出现错误

即使要将
位图
转换为
可绘制
,例如:

Drawable d = new BitmapDrawable(getResources(), bmpFinal);
它仍然会给您一个错误,因为
Drawable
在您的应用程序包中不存在

唯一可能的解决方案是使用
包中存在的
可绘制的
资源
,并将其设置为
setSmallIcon()
方法。典型用法:

builder.setSmallIcon(R.drawable.ic_launcher);
或者,需要一个位图实例。无需对当前代码进行任何其他更改(因为您已经有了
位图
),如果它符合您的要求,您可以按原样使用它


如果没有,您几乎必须使用一个
Drawable
资源,该资源已经存在于一个
Drawable
文件夹中。

您可以尝试使用此方法

 builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));

这个问题有一些要点,主要与API 23+有关,如果您只对setSmallIcon感兴趣,请转到第2和第3个主题

第一:

您可以从可绘图(而不是资源id)设置LargeIcon,如下所示

Drawable drawable= ContextCompat.getDrawable(this,R.drawable.your_drawable);

            Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
                            .setLargeIcon(bitmap)
                            .setContentTitle("hahah")
                            .setContentText("Tap to stop")
                            .setOngoing(true);
第二:

如果需要在API中设置一个低于23的小图标,则需要设置一个资源id,如
R.drawable.your_resource
NotificationCompat.Builder
不允许您在
setSmallIcon()
中使用绘图或位图

第三:

幸运的是,在23+版中,使用Notification.Builder,支持已扩展到
Icon
键入
setSmallIcon()
,如下所示:

 Drawable drawable = ContextCompat.getDrawable(this,R.drawable.your_drawable);

            Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

            Notification.Builder mBuilder =
                    new Notification.Builder(context)
                            .setSmallIcon(Icon.createWithBitmap(bitmap))
                            .setLargeIcon(bitmap)
                            .setContentTitle("hahah")
                            .setContentText("Tap to stop")
                            .setOngoing(true);

更好的选择获取应用程序图标

 Drawable drawable=getApplicationInfo().loadIcon(getPackageManager());
 Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();



.setSmallIcon(getApplicationInfo().icon)
.setLargeIcon(bitmap)

... 把我的答案写在这里,我不知道为什么。。右侧的小图标称为大图标。。只有在那里才能设置位图。在更高的api中,您可以使用自定义布局创建自己的通知。您是否尝试过使用此选项?通知通知=新通知(R.drawable.logo,“App”,System.currentTimeMillis());这并没有回答OP的问题。感谢上一个问题,尽管旧版本仍然没有解决方案。。很高兴知道anywaysExcellent选项,但要小心,如果图标是自适应图标,应用程序会崩溃,因为android工具栏不支持它们。