错误通知:给定区域必须与位图的尺寸相交

问题描述 投票:1回答:1

对于少量用户,我在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添加了一些针对此错误的日志,希望我很快就能获得更多数据。

android android-notifications android-bitmap android-image
1个回答
0
投票

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

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");
}

这应该可以在设备中使用,通知中包含两个图标。

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