a-student / BetterVectorDrawable

The VectorDrawable implementation for Android 4.0+
151 stars 22 forks source link

RemoteServiceException when using in Notifications #7

Closed krawa closed 8 years ago

krawa commented 8 years ago

I get an crash when I use the BetterVectorDrawable in Notifications

NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_statusbar_vector) 
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis())
                .setContentTitle(context.getString(R.string.app_name))
                .setDefaults(Notification.DEFAULT_LIGHTS) ;
.............................
Notification notification = nb.build();
manager.notify(lastId, notification);

StackTrace

Report:
APP_VERSION_CODE=
APP_VERSION_NAME=
PHONE_MODEL=GT-I9505
ANDROID_VERSION=4.4.2
CUSTOM_DATA=
STACK_TRACE=android.app.RemoteServiceException: Bad notification posted from package com.my.appname: Couldn't create icon: StatusBarIcon(pkg=com.my.appnameuser=0 id=0x7f0204a4 level=0 visible=true num=0 )
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
a-student commented 8 years ago

I do not think it is possible to use vector drawables in notifications because notification icons are handled by system and cannot be intercepted. Workaround is to use PNG for this particular drawable. For large icon you could convert vector drawable to a bitmap via BitmapUtil and to setLargeIcon.

krawa commented 8 years ago

Where else can not use BetterVectorDrawable?

a-student commented 8 years ago

What places do you mean?

krawa commented 8 years ago

Notifications and maybe widgets. What relates to RemoteViews.

a-student commented 8 years ago

You may try to inherit from the RemoteViews class and override the apply method:

@Override
public View apply(Context context, ViewGroup parent) {
    VectorDrawableCompat.enableResourceInterceptionFor(context.getResources(),
            R.drawable.airplane_vector,
            R.drawable.bicycle_vector,
            R.drawable.lamp_vector,
            R.drawable.rocket_vector);
    return super.apply(context, parent);
}

Presumably this enables resource interception. But I did not test it.

a-student commented 8 years ago

Did this help?

krawa commented 8 years ago

Sorry, I did not try. I do not use the BetterVectorDrawable in my project.

luhmirin-s commented 8 years ago

I got same problem and experimented with workarounds you mentioned. One from Nov 26 did not work for me and felt a bit wrong. I ended up using BitmapUtil. Here is a snippet:

public ClassConstructor(Context context) {
  this.context = context;
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
    this.metrics = context.getResources().getDisplayMetrics();
}

private void createRemoteView() {
  expandedRemoteView = new RemoteViews(packageName, R.layout.notification);
  // some initialization code 
  setImage(
    expandedRemoteView, 
    R.id.player_notification_big_jump_back, 
    R.drawable.vector_notification_backward
  );
  // some more code
}

private void setImage(RemoteViews remoteView, @IdRes int imageViewId, @DrawableRes int imageId) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     remoteView.setImageViewResource(imageViewId, imageId);
  } else {
    remoteView.setImageViewBitmap(
        imageViewId,
        BitmapUtil.toBitmap(ContextCompat.getDrawable(context, imageId), metrics)
    );
  }
}

In my case I use RemoteViews for custom layout in notification, but it should be possible to add notification icon in similar way.

a-student commented 8 years ago

siga111, thank you for providing solution!