Glide:加载图片以推送通知

11

我想使用Glide加载图片到推送通知中,但出现以下错误:

FATAL EXCEPTION: Thread-9730
Process: com.monkingme.monkingmeapp, PID: 24226
java.lang.IllegalArgumentException: You must call this method on the main thread at com.bumptech.glide.util.Util.assertMainThread(Util.java:135)                                                                                

并且使用的代码:

NotificationTarget notificationTarget = new NotificationTarget(
                context,
                rv,
                R.id.remoteview_notification_icon,
                notification,
                NOTIFICATION_ID);

Glide.with(context.getApplicationContext())
     .load(item.getString("cover_img"))
     .asBitmap()
     .placeholder(placeholder)
     .error(placeholder)
     .into(notificationTarget);

我正在使用 Aerogear 中的 MessageHandler-->https://aerogear.org/docs/guides/aerogear-android/push/

问题是,在推送通知中应用程序未运行,因此没有主线程。有什么建议吗?


1
那是一个很棒的问题。 - Ahmed Ali
1
从Glide 4.9.0开始,加载主线程外的图像应该可以直接使用!https://github.com/bumptech/glide/commit/8f1ea5c07dff7ade8c49c324bcb5a7f40d0b4891 - yuval
4个回答

8
尝试这种方法:
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override 
            public void run() {
                 Glide.with(context.getApplicationContext())
                    .load(item.getString("cover_img"))
                    .asBitmap()
                    .placeholder(placeholder)
                    .error(placeholder)
                    .into(notificationTarget);
        }
    });

6

这是可以预料的。由于图像是从互联网加载的,因此应始终位于 async 调用或后台线程中。您可以使用异步任务或像Glide这样的图像加载库。

要从url中加载图像通知,可以使用样式 "NotificationCompat.BigPictureStyle()"。这需要一个位图 (必须从图像url中提取)

大多数Glide的API和方法现在已过时。以下内容适用于Glide 4.9及Android 10及以下版本。

 // Load bitmap from image url on background thread and display image notification
        private void getBitmapAsyncAndDoWork(String imageUrl) {

            final Bitmap[] bitmap = {null};

            Glide.with(getApplicationContext())
                    .asBitmap()
                    .load(imageUrl)
                    .into(new CustomTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                            bitmap[0] = resource;
                            // TODO Do some work: pass this bitmap
                            displayImageNotification(bitmap[0]);
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                        }
                    });
        }

当位图准备就绪时,只需显示一次图像通知。

private void displayImageNotification(Bitmap bitmap) {

      NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), getChannelId());
            builder
                    .setContentTitle(title)
                    .setContentText(subtext)
                    .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
                    .setSmallIcon(SMALL_ICON)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setColor(getApplicationContext().getColor(color))
                    .setAutoCancel(true)
                    .setOngoing(false)
                    .setOnlyAlertOnce(true)
                    .setContentIntent(pendingIntent)
                     .setStyle(
                     new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
                    .setPriority(Notification.PRIORITY_HIGH);

        getManager().notify(tag, id, builder.build());
}

1
这种方法会在加载图像失败时导致一些错误。或者当线程终止时(例如在广播接收器中),没有回调被调用,你应该创建一种处理这些情况的方法。 - Amir Hossein Ghasemi

1

对于那些可能对Glide和Kotlin的最新版本感到困惑的人:

val target = NotificationTarget(
            context,
            R.id.iv_image,
            remoteView,
            notification,
            NOTIFICATION_ID)

Glide.with(context.applicationContext)
        .asBitmap()
        .load(url)
        .into(target)

重要提示:在with(context)之后应立即使用asBitmap()

1
我的解决方案:
public class NotificationBuilder {

  public static void build(Context context) {
    ...
    NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.app_icon_notification).setContentTitle("Title")
            .setContentText("Description").setAutoCancel(true).setShowWhen(true)
            .setWhen(1574521462).setLights(ledColor, 200, 2000)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setStyle(new NotificationCompat.BigTextStyle().bigText("Description"))
            .setTicker("Description").setSound(defaultSoundUri).setContentIntent(pendingIntent);
    FutureTarget<Bitmap> futureTarget = GlideApp.with(context).asBitmap()
        .load("http://example.com/myImage.jpg")
        .circleCrop().submit();
    LoadImageTask task = new LoadImageTask(icon -> {
          notificationBuilder.setLargeIcon(icon);
          GlideApp.with(context).clear(futureTarget);
          notificationManager.notify(NotificationsCons.SUPPORT_MESSAGES_NOTIFICATION_ID,
              notificationBuilder.build());
        });
    task.execute(futureTarget);
  }
}

private static class LoadImageTask extends AsyncTask<FutureTarget<Bitmap>, Void, Bitmap> {
  private OnSuccess onSuccess;

  interface OnSuccess {
    void onSuccess(Bitmap bitmap);
  }

  LoadImageTask(OnSuccess onSuccess) {
    this.onSuccess = onSuccess;
  }

  @SafeVarargs @Override
  protected final Bitmap doInBackground(FutureTarget<Bitmap>... futureTargets) {
    try {
      return futureTargets[0].get();
    } catch (ExecutionException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return null;
  }

  @Override protected void onPostExecute(Bitmap bitmap) {
    super.onPostExecute(bitmap);
    if (bitmap != null)
      onSuccess.onSuccess(bitmap);
  }
}

它运行正常。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接