Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification throws error when using vector drawables

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

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 )

Here is my code :

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); 

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 : 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.

like image 630
ShahiM Avatar asked May 19 '16 21:05

ShahiM


People also ask

How to use vector drawable in Android?

To configure your app to use vector support libraries, add the vectorDrawables element to your build. gradle file in the app module. To change drawables at runtime, you can use the setImageResource() method as before.

What is vector asset in Android studio?

Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.

Can vector Drawables be scaled?

In Android 5.0 (API Level 21) and above, you can define vector drawables, which scale without losing definition. Changing the width and height in the definition of the vector drawable to 200dp significantly improves the situation at the 400dp rendered size.

What is vector drawable compat?

VectorDrawableCompat. For API 24 and above, this class delegates to the framework's VectorDrawable . For older API version, this class lets you create a drawable based on an XML vector graphic. You can always create a VectorDrawableCompat object and use it as a Drawable by the Java API.


1 Answers

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.

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.

like image 50
CommonsWare Avatar answered Sep 17 '22 08:09

CommonsWare