使用矢量drawable时,通知会引发错误

Sha*_*hiM 25 java android android-appcompat android-notifications android-support-library

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

android.app.RemoteServiceException:从包com.qbes.xxx发布的错误通知:无法创建图标: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);
Run Code Online (Sandbox Code Playgroud)

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

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'
}
Run Code Online (Sandbox Code Playgroud)

PS:当我使用png或jpg图像绘制时,代码工作正常,但在使用矢量drawable时断开.

我一直在寻找一整天但找不到任何对我有用的东西.有任何想法吗.

Com*_*are 39

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

您可以将矢量绘制自己呈现给a Canvas支持Bitmap,然后Bitmap在中使用Notification.或者,您可以使用矢量backport库,但是对于您的少数Notification图标,为它们生成PNG文件并在旧设备上使用它们.将相应的矢量绘图放入res/drawable-anydpi-v21/,较新的设备将使用矢量绘图,而较旧的设备将回退到PNG.