使用矢量drawable时,通知会引发错误 [英] Notification throws error when using vector drawables

查看:290
本文介绍了使用矢量drawable时,通知会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用vector drawable设置通知的小图标时,我得到以下异常:

I get the following exception when I use a vector drawable to set the small icon for a notification :


android.app.RemoteServiceException :从com.qbes.xxx包发布错误通知:无法创建图标:StatusBarIcon(pkg = com.qbes.xxxuser = 0 id = 0x7f020082 level = 0 visible = true num = 0)

android.app.RemoteServiceException: Bad notification posted from package com.qbes.xxx: Couldn't create icon: StatusBarIcon(pkg=com.qbes.xxxuser=0 id=0x7f020082 level=0 visible=true num=0 )

这是我的代码:

mNotificationBuilder = new android.support.v4.app.NotificationCompat.Builder(this)
                .setDefaults(android.support.v4.app.NotificationCompat.DEFAULT_LIGHTS)
                .setSound(null)
                .setSmallIcon(R.drawable.logo_white)
                .setColor(getResources().getColor(R.color.colorPrimary))
                .setCategory(android.support.v4.app.NotificationCompat.CATEGORY_PROGRESS)
                .setContentTitle("Trip in Progress...")
                .setAutoCancel(false)
                .setProgress(0, 0, progress)
                .setOngoing(true)
                .setPriority(android.support.v4.app.NotificationCompat.PRIORITY_MAX)
                .setOnlyAlertOnce(true)
                .setContentIntent(pendingIntent);

mNotificationBuilder.setContentText(body);

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = mNotificationBuilder.build();

mNotificationManager.notify(Constants.NOTIFICATION_ID_Dash, note);

和我的 build.gradle (仅相关)部分):

and my build.gradle (only relevant parts) :

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.qbes.xxx"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 720
        versionName "0.7.20"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
}

PS:代码有效当我使用png或jpg图像绘制时很好,但在使用矢量drawable时会中断。

PS : The code works fine when I use a png or jpg image drawable, but breaks when using a vector drawable.

我一直在寻找一整天,却找不到任何对我有用的东西。任何想法。

I have been searching for a whole day but could not find anything that worked for me. Any Ideas.

推荐答案

您正在使用vector drawable支持包。这很好,但只适用于您的应用。该平台不知道如何在API Level 21之前使用矢量drawable,而对于 Notification ,平台就是渲染资源的平台。

You are using the vector drawable support package. That's fine, but that only works in your app. The platform does not know how to use vector drawables prior to API Level 21, and for a Notification, the platform is the one rendering the resource.

您可以将矢量绘制为自己渲染到 Canvas 位图支持,然后在通知中使用位图。或者,您可以使用矢量反向移植库,但是对于少量的通知图标,为它们生成PNG文件并在旧设备上使用它们。 将相应的矢量绘图放入 res / drawable-anydpi-v21 / ,较旧的设备将使用矢量绘图,而较旧的设备会回退到PNG。

You could render the vector drawable yourself to a Canvas backed by a Bitmap, then use that Bitmap in the Notification. Or, you could use the vector backport library in general, but for your handful of Notification icons, generate PNG files for them and use them on the older devices. Put the corresponding vector drawables in res/drawable-anydpi-v21/, and the newer devices will use the vector drawables while older devices fall back to the PNGs.

这篇关于使用矢量drawable时,通知会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆