如何使用 url 图像通知 Android 中的 LargeIcon?

how can use url image to notification LargeIcon in Android?

如何使用 url 图片通知 Android 中的 LargeIcon?

我是这样用的,大图标无法显示

 Map<String, String> data = remoteMessage.getData();
bigPhoto = data.get("photo").toString();

            sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), fid,bigPhoto);

 private void sendNotification(String messageTitle,String messageBody, String fid,String bigPhoto) {

Bitmap largeIcon = BitmapFactory.decodeFile(bigPhoto);
        Log.d("DDD", bigPhoto);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setContentTitle(messageTitle)
                        .setSmallIcon(R.drawable.luvtas_au3)
                        .setLargeIcon(largeIcon)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                        .setGroup(channelId)
                        .setContentIntent(pendingIntent);
         notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

我可以获取图像 URL,但它没有显示在通知中。

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: https:/itinerary.colatour.com.tw/COLA_AppFiles/A03A_Tour/PictureObj/00073106.JPG (No such file or directory)

所以哪里有问题?

BitmapFactory.decodeFile() 仅适用于解码本地文件。当您处理远程地址时,您应该使用像 Glide 这样的图像加载库下载图像,并将解码后的位图传递给通知生成器对象。这是一个使用阻塞方法将图像下载为位图的简单示例:

private Bitmap getLargeIcon(Context context, String picturePath) {
    if (picturePath != null) {
        try {
            return Glide.with(context)
                    .as(Bitmap.class)
                    .load(picturePath)
                    .get(4000, TimeUnit.MILLISECONDS);
        } catch (ExecutionException | InterruptedException | TimeoutException ignored) {}
    }
    return null;
}

并在您的通知函数中调用方法:

Bitmap largeIcon = getLargeIcon(this, bigPhoto);

注意: 由于 getLargeIcon 使用阻塞方法下载您的图像,请不要忘记在工作线程上调用您的 sendNotification 函数。