【问题标题】:new NotificationCompat.BigPictureStyle() show error couldn't inflate view for notification新 NotificationCompat.BigPictureStyle() 显示错误无法为通知充气视图
【发布时间】:2018-01-19 07:11:45
【问题描述】:

我想在通知栏中显示我正在使用此代码的大尺寸图像

我的图像来自 server.so 在 AsyncTask 中将其转换为位图并在活动图像视图中显示其工作正常,但是当使用通知生成器在通知中加载相同的位图时显示错误

 Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);
                imgView.setImageBitmap(img);
  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .setContentTitle("hahahha").setStyle(new NotificationCompat.BigPictureStyle()
                                .bigPicture(img))
                        .setAutoCancel(true);
   NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
   notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
   01-19 12:28:32.556 1737-1737/? W/ProgressBarDelegate: Unknown Drawable subclass, src=android.graphics.drawable.ScaleDrawable@3c75db5
        01-19 12:28:32.590 1737-1737/? E/StatusBar: couldn't inflate view for notification com.synergy.fcmdemo/0x0
   android.widget.RemoteViews$ActionException: view: android.widget.FrameLayout doesn't have method: setEmphasizedMode(boolean)
   android.widget.RemoteViews.getMethod(RemoteViews.java:851)
   android.widget.RemoteViews.-wrap5(RemoteViews.java)
   android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1411)
   android.widget.RemoteViews.performApply(RemoteViews.java:3425)
   android.widget.RemoteViews.apply(RemoteViews.java:3160)
   com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:615)
   com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:1036)
   at com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:1015)
   at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:2088)
   at com.android.systemui.statusbar.CommandQueue$H.handleMessage(CommandQueue.java:472)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:163)
   at android.app.ActivityThread.main(ActivityThread.java:6205)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
   01-19 12:28:32.591 1737-1737/? E/StatusBar: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.synergy.fcmdemo user=UserHandle{0} id=0 tag=null key=0|com.synergy.fcmdemo|0|null|10137: Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE))
   01-19 12:28:32.594 1737-1737/? D/StatusBar: onNotificationRemoved:  Key: 0|com.synergy.fcmdemo|0|null|10137

【问题讨论】:

  • 你是怎么解决的?我遇到了同样的错误。

标签: java android firebase bitmap push-notification


【解决方案1】:

您从 api 响应中获取图像并转换为 Bitmap,但在您的通知构建器中添加了可绘制的图像,只需更改此设置

Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);              
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(img)
                    .setContentTitle("hahahha").setStyle(new NotificationCompat.BigPictureStyle()
                            .bigPicture(img))
                    .setAutoCancel(true); 

现在要设置大图样式

Notification notif = new Notification.Builder(context)
     .setContentTitle("hahahha")
     .setContentText("content")
     .setSmallIcon(R.drawable.ic_launcher)
     .setLargeIcon(bitmap)
     .setStyle(new Notification.BigPictureStyle()
     .bigPicture(img)
     .setBigContentTitle("hahahha"))
     .build();

【讨论】:

    【解决方案2】:

    试试他的,为我工作

    使用线程从服务器获取图片

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                showBigNotification();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
    

    发送通知

    private void showBigNotification() {
        Bitmap bitmap = getBitmapFromURL(**Your image url**);
    
        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
        bigPictureStyle.setBigContentTitle("title");
        bigPictureStyle.setSummaryText("message");
        bigPictureStyle.bigPicture(bitmap);
    
        Intent notificationIntent = new Intent(mContext, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext,
                    101, notificationIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);
    
        NotificationManager nm = (NotificationManager) mContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
    
        Resources res = mContext.getResources();
        Notification.Builder builder = new Notification.Builder(mContext);
    
        builder.setContentIntent(contentIntent)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
                    .setTicker(res.getString(R.string.app_name))
                    .setStyle(bigPictureStyle)
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setContentTitle("Title")
                    .setContentText("Message");
        Notification n = builder.build();
    
        if (nm != null) {
            nm.notify(1001, n);
        }
    }
    

    从服务器获取图片

    public Bitmap getBitmapFromURL(String strURL) {
        try {
            URL url = new URL(strURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            return BitmapFactory.decodeStream(input);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 2017-03-12
      • 2021-10-14
      • 1970-01-01
      • 2021-11-13
      • 2014-11-10
      相关资源
      最近更新 更多