如何减少通知占用的内存使用量?

时间:2017-11-17 08:03:48

标签: java android out-of-memory android-notifications remoteview

有了以下内容,我正在尝试创建一个通知,但在某个地方我得到了一个OOM,我怀疑largeIcon可能太大了。

如果这是问题,是否可以减少放入通知的图像大小?

通知::

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel();
        }
        createSession();
        updateMediaSessionMetaData(song);
        MediaControllerCompat      mediaControllerCompat  = session.getController();
        MediaMetadataCompat        mediaMetadataCompat    = mediaControllerCompat.getMetadata();
        MediaDescriptionCompat     mediaDescriptionCompat = mediaMetadataCompat.getDescription();
        NotificationCompat.Builder builder                = MediaStyleHelper.from(getBaseContext(), session);
        builder.setContentTitle(mediaDescriptionCompat.getTitle());
        builder.setContentText(mediaDescriptionCompat.getSubtitle());
        builder.setContentIntent(contentIntent());
        builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        builder.setLargeIcon(mediaDescriptionCompat.getIconBitmap());
        builder.setSmallIcon(getApplicationInfo().icon);
        builder.setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
        builder.addAction(playPause());
        builder.addAction(playNext());
        builder.addAction(clear2());
        builder.addAction(playPrev());
        builder.setStyle(style());
        builder.setPriority(Notification.PRIORITY_MAX);
        builder.setDeleteIntent(clearSwipe());
        startForeground(452, builder.build());

日志

couldn't inflate view for notification com.aollay.utlwa/0x1c4
                                            android.widget.RemoteViews$ActionException: java.lang.reflect.InvocationTargetException
                                                at android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1347)
                                                at android.widget.RemoteViews.performApply(RemoteViews.java:2898)
                                                at android.widget.RemoteViews.apply(RemoteViews.java:2851)
                                                at com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:1398)
                                                at com.android.systemui.statusbar.phone.HwPhoneStatusBar.inflateViews(HwPhoneStatusBar.java:980)
                                                at com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:1331)
                                                at com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:1718)
                                                at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:1523)
                                                at com.android.systemui.statusbar.phone.HwPhoneStatusBar.addNotification(HwPhoneStatusBar.java:1427)
                                                at com.android.systemui.statusbar.BaseStatusBar$5$3.run(BaseStatusBar.java:527)
                                                at com.android.systemui.statusbar.BaseStatusBar$H.handleMessage(BaseStatusBar.java:1291)
                                                at com.android.systemui.statusbar.phone.PhoneStatusBar$H.handleMessage(PhoneStatusBar.java:2376)
                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                at android.os.Looper.loop(Looper.java:150)
                                                at android.app.ActivityThread.main(ActivityThread.java:5665)
                                                at java.lang.reflect.Method.invoke(Native Method)
                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
                                             Caused by: java.lang.reflect.InvocationTargetException
                                                at java.lang.reflect.Method.invoke(Native Method)
                                                at android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1343)
                                                at android.widget.RemoteViews.performApply(RemoteViews.java:2898) 
                                                at android.widget.RemoteViews.apply(RemoteViews.java:2851) 
                                                at com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:1398) 
                                                at com.android.systemui.statusbar.phone.HwPhoneStatusBar.inflateViews(HwPhoneStatusBar.java:980) 
                                                at com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:1331) 
                                                at com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:1718) 
                                                at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:1523) 
                                                at com.android.systemui.statusbar.phone.HwPhoneStatusBar.addNotification(HwPhoneStatusBar.java:1427) 
                                                at com.android.systemui.statusbar.BaseStatusBar$5$3.run(BaseStatusBar.java:527) 
                                                at com.android.systemui.statusbar.BaseStatusBar$H.handleMessage(BaseStatusBar.java:1291) 
                                                at com.android.systemui.statusbar.phone.PhoneStatusBar$H.handleMessage(PhoneStatusBar.java:2376) 
                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                at android.os.Looper.loop(Looper.java:150) 
                                                at android.app.ActivityThread.main(ActivityThread.java:5665) 
                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799) 
                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689) 
                                             Caused by: java.lang.OutOfMemoryError: Failed to allocate a 150994956 byte allocation with 4194304 free bytes and 129MB until OOM
                                                at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:667)
                                                at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:488)
                                                at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1082)
                                                at android.content.res.Resources.loadDrawableForCookie(Resources.java:2729)
                                                at android.content.res.Resources.loadDrawable(Resources.java:2630)
                                                at android.content.res.HwResources.loadDrawable(HwResources.java:699)
                                                at android.content.res.Resources.getDrawable(Resources.java:854)
                                                at android.graphics.drawable.Icon.loadDrawableInner(Icon.java:313)
                                                at android.graphics.drawable.Icon.loadDrawable(Icon.java:269)
                                                at android.widget.ImageView.setImageIcon(ImageView.java:498)
                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                at android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1343) 
                                                at android.widget.RemoteViews.performApply(RemoteViews.java:2898) 
                                                at android.widget.RemoteViews.apply(RemoteViews.java:2851) 
                                                at com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:1398) 
                                                at com.android.systemui.statusbar.phone.HwPhoneStatusBar.inflateViews(HwPhoneStatusBar.java:980) 
                                                at com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:1331) 
                                                at com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:1718) 
                                                at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:1523) 
                                                at com.android.systemui.statusbar.phone.HwPhoneStatusBar.addNotification(HwPhoneStatusBar.java:1427) 
                                                at com.android.systemui.statusbar.BaseStatusBar$5$3.run(BaseStatusBar.java:527) 
                                                at com.android.systemui.statusbar.BaseStatusBar$H.handleMessage(BaseStatusBar.java:1291) 
                                                at com.android.systemui.statusbar.phone.PhoneStatusBar$H.handleMessage(PhoneStatusBar.java:2376) 
                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                at android.os.Looper.loop(Looper.java:150) 
                                                at android.app.ActivityThread.main(ActivityThread.java:5665) 
                                                at java.lang.reflect.Method.invoke(Native Method) 

错误通知

android.app.RemoteServiceException: Bad notification posted from package com.aollay.utlwa: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.aollay.utlwa user=UserHandle{0} id=452 tag=null score=20 key=0|com.aollay.utlwa|452|null|10180: Notification(pri=2 contentView=com.aollay.utlwa/0x109008b vibrate=null sound=null defaults=0x0 flags=0x62 color=0xff333745 category=transport actions=4 vis=PUBLIC))
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1542)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:150)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5665)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)

0 个答案:

没有答案