【问题标题】:Bad notification: The given region must intersect with the Bitmap's dimensions错误通知:给定区域必须与位图的尺寸相交
【发布时间】:2019-03-01 16:43:43
【问题描述】:

我在 Crashlytics 上遇到了一小部分用户的例外情况。对于大多数用户来说,它似乎可以正常工作,但对于某些用户来说却失败了(数量很少,但我现在只是在分阶段发布)。我认为问题可能是我缩小了我在通知中使用的位图,我使用这个代码来缩小它:

bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);

例外是这样的:

Fatal Exception: android.app.RemoteServiceException: Bad notification posted from package mypacakge: Couldn't inflate contentViewsjava.lang.IllegalArgumentException: The given region must intersect with the Bitmap's dimensions.
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
           at android.os.Handler.dispatchMessage(Handler.java:105)
           at android.os.Looper.loop(Looper.java:164)
           at android.app.ActivityThread.main(ActivityThread.java:6940)
           at java.lang.reflect.Method.invoke(Method.java)
           at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

我做错了什么?

谢谢。

编辑:我搜索了 Android 代码以查看谁抛出了该异常,它似乎是 Palette 构建器,然后我搜索了与使用 PalettesetRegion() 方法的通知相关的代码并发现这个在MediaNotificationProcessor.java:

Palette.Builder paletteBuilder = Palette.from(bitmap)
                        .setRegion(0, 0, bitmap.getWidth() / 2, bitmap.getHeight())
                        .clearFilters() // we want all colors, red / white / black ones too!
                        .resizeBitmapArea(RESIZE_BITMAP_AREA);
                Palette palette = paletteBuilder.generate();
                backgroundColor = findBackgroundColorAndFilter(palette);
                // we want most of the full region again, slightly shifted to the right
                float textColorStartWidthFraction = 0.4f;
                paletteBuilder.setRegion((int) (bitmap.getWidth() * textColorStartWidthFraction), 0,
                        bitmap.getWidth(),
                        bitmap.getHeight());
                if (mFilteredBackgroundHsl != null) {
                    paletteBuilder.addFilter((rgb, hsl) -> {
                        // at least 10 degrees hue difference
                        float diff = Math.abs(hsl[0] - mFilteredBackgroundHsl[0]);
                        return diff > 10 && diff < 350;
                    });
                }

所以我的猜测是,在这里它传递了一些不好的价值,但我不知道是什么。我为这个错误添加了更多的 Fabric 日志,希望我能尽快获得更多数据。

【问题讨论】:

  • 你为什么要缩小规模?仅根据要求为位图图标创建一个新图标,用于通知目的。
  • 我有一些手机在拨打notify() 时发生了崩溃,包裹太大或类似的情况,所以我认为问题出在位图上,所以我决定将其缩放为测试.虽然这是一个早期的测试版,但我没有再遇到其他崩溃(包裹一个),所以现在确定可能还为时过早。
  • 如果开发者明确希望一个大图标也有一个小图标,那么我们必须只为大图标提供位图图像作为参数。而且大图标根本不是必须的,你也可以忽略它,如果你也想要大图标,那就不要缩小
  • 通知上的大图标很小,我放在那里的图像非常大,本来是要在手机的全屏上显示的。

标签: android android-notifications android-bitmap android-image


【解决方案1】:

这是一个有效的代码,希望它对你或其他人有所帮助......

String CHANNEL_ID = "my_channel_xy";
CharSequence name = "my_channel";
String Description = "This is my channel";
int NOTIFICATION_ID = 19920901; 

Log.d("FileCreator : ","Notification Creating...:"+NOTIFICATION_ID);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
      int importance = NotificationManager.IMPORTANCE_HIGH;
      NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
      mChannel.setDescription(Description);
      mChannel.enableLights(true);
      mChannel.setLightColor(Color.RED);
      mChannel.enableVibration(true);
      mChannel.setVibrationPattern(new long[]{100});
      mChannel.setShowBadge(true);

      if (notificationManager != null)
      {
           notificationManager.createNotificationChannel(mChannel);
      }

}

Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("MYVAR", "MYVAL");
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

//resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle("NextTitle")
                    .setContentText("NextText")
                    .setTicker("NextText")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText("NextMessage it is a long text which is replaced when user swipes it down from the notification tray..."))
                    .setSmallIcon(R.drawable.ic_mynotification)
                    .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
                    .setContentIntent(resultPendingIntent)
                    .setAutoCancel(false)
                    .setOngoing(true)
                    .setOnlyAlertOnce(true)
                    .setColor(getResources().getColor(android.R.color.holo_red_dark));

if (notificationManager != null)
{
      notificationManager.notify(NOTIFICATION_ID, builder.build());
      Log.d("FileCreator : ", "NOTIFIED TO THE USER... \n");
}
else
{
      Log.d("FileCreator : ", "COULD NOT BE NOTIFIED TO THE USER...????!!!! \n");
}

这应该适用于所有设备,通知中的两个图标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2021-11-08
    • 1970-01-01
    相关资源
    最近更新 更多