使用矢量可绘制对象时通知抛出错误
Notification throws error when using vector drawables
当我使用矢量可绘制对象为通知设置小图标时出现以下异常:
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
(仅相关部分):
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 图像可绘制对象时,代码工作正常,但在使用矢量可绘制对象时会中断。
我已经搜索了一整天,但找不到任何适合我的东西。任何想法。
您正在使用矢量绘图支持包。很好,但这只适用于您的应用程序。平台不知道如何在 API 级别 21 之前使用矢量可绘制对象,对于 Notification
,平台是渲染资源的平台。
您可以自己将矢量可绘制对象渲染为由 Bitmap
支持的 Canvas
,然后在 Notification
中使用该 Bitmap
。或者,您通常可以使用矢量反向移植库,但对于少数 Notification
图标,为它们生成 PNG 文件并在旧设备上使用它们。 Put the corresponding vector drawables in res/drawable-anydpi-v21/
,较新的设备将使用矢量可绘制对象,而较旧的设备将回退到 PNG。
当我使用矢量可绘制对象为通知设置小图标时出现以下异常:
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
(仅相关部分):
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 图像可绘制对象时,代码工作正常,但在使用矢量可绘制对象时会中断。
我已经搜索了一整天,但找不到任何适合我的东西。任何想法。
您正在使用矢量绘图支持包。很好,但这只适用于您的应用程序。平台不知道如何在 API 级别 21 之前使用矢量可绘制对象,对于 Notification
,平台是渲染资源的平台。
您可以自己将矢量可绘制对象渲染为由 Bitmap
支持的 Canvas
,然后在 Notification
中使用该 Bitmap
。或者,您通常可以使用矢量反向移植库,但对于少数 Notification
图标,为它们生成 PNG 文件并在旧设备上使用它们。 Put the corresponding vector drawables in res/drawable-anydpi-v21/
,较新的设备将使用矢量可绘制对象,而较旧的设备将回退到 PNG。