问题描述
在写Android应用的过程中,需要在Notification的RemoteViews中添加网络图片,并做异步加载。
思路
使用三方异步加载库Glide
代码
如下
NotificationTarget notificationTarget =
new NotificationTarget(
this, //Context上下文
R.id.notification_album, //RemoteViews的layout中具体的,你希望填充的ImageView的id
remoteViews, //RemoteViews的实例对象
notification, //Notification的实例对象
NOTIFICATION_ID); //NotificationManager.notify()或者startForeground()方法的第一个参数,建议定义一个自定义的变量NOTIFICATION_ID,随便等于多少,我这里是123,自定义的
//imagePath为在线图片的url,或者本地图片的绝对路径也可以
Glide.with(this).asBitmap().load(imagePath).into(notificationTarget);
参考了如下文章:
https://www.jianshu.com/p/47686ce9d9a4?nomobile=yes
但是他的NotificationTarget构造函数的参数顺序和我写的时候不一样,我看源码的构造函数,然后修改的。写的时候,要根据自己的具体情况变通。