Glide和NotificationCompat.Builder setLargeIcon()

问题描述 投票:6回答:2

如何使用Glide进入NotificationCompat.Builder setLargeIcon(Bitmap icon)?我已经调查了这个tutorial,但我不想使用RemoteViews。我也想使用Glide.placeholder(int resource)Glide.error(int resource)而不使用策略Glide.into(new SimpleTarget<Bitmap>(){ ... });

android bitmap notifications android-notifications android-glide
2个回答
2
投票

最后我没有找到方法,所以我做了战略Glide.into(new SimpleTarget<Bitmap>(){ ... });,这是:

int largeIconSize = Math.round(64 * context.getResources().getDisplayMetrics().density);
GlideApp.with(context)
        .asBitmap()
        .load(largeIconUrl)
        .override(largeIconSize, largeIconSize)
        .placeholder(placeHolderResource)
        .into(new BaseTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                notificationBuilder.setLargeIcon(resource);
                publish();
            }

            @Override
            public void getSize(SizeReadyCallback cb) {
                cb.onSizeReady(largeIconSize, largeIconSize);
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                super.onLoadFailed(errorDrawable);
                notificationBuilder.setLargeIcon(((BitmapDrawable) errorDrawable).getBitmap());
                publish();
            }

            @Override
            public void onLoadStarted(@Nullable Drawable placeholder) {
                super.onLoadStarted(placeholder);
                notificationBuilder.setLargeIcon(((BitmapDrawable) placeholder).getBitmap());
                publish();
            }
        });

publish()是:

Notification notification = notificationBuilder.build();
notificationManager.notify(notificationID, notification);

1
投票

这是我用Glide 4.8.0做到的

val notificationBuilder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_message)
            .setContentTitle("title")
            .setContentText("text")

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

val futureTarget = Glide.with(this)
            .asBitmap()
            .load(photoUrl)
            .submit()

val bitmap = futureTarget.get()
notificationBuilder.setLargeIcon(bitmap)

Glide.with(this).clear(futureTarget)

notificationManager.notify(0, notificationBuilder.build())

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.