android notification large image not working

时光毁灭记忆、已成空白 提交于 2019-12-04 08:05:41

Android icons (and other UI elements, like drag lengths) are measured in dp. A dp is a device/density-independent pixel. 1 dp is equivalent to 1 px on a 160 dpi screen. But to convert to other screen densities, you need to multiply it by a density factor. So it's generally recommended that multiple images are supplied for most icons.

For example, the notification icons used in the status bar are specified as 24x24 dp, with a 1 dp margin (so the actual icon only takes up a 22x22 dp optical square, though some of the AA can bleed into that 1 dp margin/safeframe). To convert 24 dp to actual pixel sizes, these rough calculations are used:

display density  dp units * scale = px units
ldpi  ~120 dpi   24x24 dp * .75   = 18x18 px
mdpi  ~160 dpi   24x24 dp * 1.0   = 24x24 px
hdpi  ~240 dpi   24x24 dp * 1.5   = 36x36 px
xhdpi ~320 dpi   24x24 dp * 2.0   = 48x48 px

There's also an intermediate display density called tvdpi (~213 dpi) that sits between mdpi and hdpi and has a scale factor of 1.33, but this is much less common. What the Android docs recommend is that you follow a 3:4:6:8 scaling ratio when providing prescaled bitmap images (usually PNGs) for the most common display densities.

I don't see anywhere where they specify the dp size for the large icons used in notifications, but the height of each notification in normal inbox view is 64 dp. So that means the max size for icons/images shown there would be:

ldpi:     48x48 px
mdpi:     64x64 px
hdpi:     96x96 px
xhdpi:  128x128 px

If you want to know exactly what image sizes Android's stock icons are, you should be able to find out from the Android Icon Templates Pack, v4.0.

I had the same probme cause i missinterpreted the calls for SetSmallIcon and SetLargeIcon. You HAVE to specify a small icon, otherwise the notification is not shown. The big icon is optional, and if not set, the small icon is used.

I think you should decode the bitmap before asking for it in the Builder, like this:

 Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.unknown);
 Intent intent = new Intent(this, OfferNotification.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0,
                intent, 0);
        Uri soundUri = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.unknown)
                //.setLargeIcon(bitmap)
                .addAction(R.drawable.ic_launcher, "d", pIntent)
                .setAutoCancel(true)
                .setContentTitle("Offer from " + restaurantName)
                .setContentText(offerDescriptoin).setSound(soundUri);

It is perhaps not decoding properly or in time. It also would eliminate one unknown here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!