我需要通过来自外部 URL 的 PNG 而不是使用 mipmap 来设置小的 Android 通知图标。
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.mipmap.ic_notification);
notificationBuilder.setColor(ContextCompat.getColor(getApplicationContext(), android.R.color.transparent));
}
需要传递外部PNG图像而不是使用:R.mipmap.ic_notification
有人知道这是否可行吗?
最佳答案
如果您阅读特定于 Notification.Builder 的开发人员文档,您会看到 setSmallIcon(int icon)
需要应用程序包中的资源 ID 才能使用。
下载图像,转换为位图,然后将其设置为 setSmallIcon(int resId)
仍然会给您一个错误。
即使您要像这样将 Bitmap 转换为 Drawable:
Drawable d = new BitmapDrawable(getResources(), bmpFinal);
它仍然会给你一个错误,因为你的应用程序包中不存在该 Drawable。
唯一可能的解决方案是使用包中存在的 Drawable 资源并将其设置为 setSmallIcon()
方法。典型用法:
builder.setSmallIcon(R.drawable.ic_launcher);
或者,setLargeIcon(位图图标)
需要一个位图实例。无需对当前代码进行任何其他更改(因为您已经有了位图),如果它符合您的要求,您可以按原样使用它。
如果没有,您几乎必须使用已存在于其中一个可绘制文件夹中的 Drawable
资源。
关于android - Android通知中来自外部网址的setSmallIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47949620/