首页 > 解决方案 > 从 StatusBarNotification 检索小图标作为位图

问题描述

我正在尝试在 NotificationListenerService.

我找到了notification.getSmallIcon()返回 a 的函数android.graphics.drawable.Icon。怎么可能Bitmap从那个图标获得一个?

标签: androidbitmap

解决方案


您需要在图标上使用loadDrawableloadDrawableAsync,它将返回一个可绘制对象。

loadDrawable在 API 级别 23 中添加public Drawable loadDrawable (Context context)返回Drawable可用于在此图标内绘制图像的 a,必要时构建它。根据图像的类型,这可能不是您希望在 UI 线程上执行的操作,因此请考虑改用 loadDrawableAsync。

如下所示

Icon icon = notification.getSmallIcon(); 
Bitmap bitmap = icon.loadDrawable(context);

然后您可以从下面的stackoverflow链接将drawable转换为位图有很多答案如何将drawable转换为位图

如何将 Drawable 转换为位图?


推荐阅读