简体   繁体   中英

Android notification action icon: Resources not found exception

I am currently working on an android app which recreates the notifications from the notification center. To fully recreate the notification experience i also tried making the notification actions available in my app (eg. the back/play/pause/next buttons from music notifications).

Icon icon = notification.actions[0].getIcon();
((ImageView)view.findViewById(R.id.action0)).setImageIcon(icon);

But i get a resource not found exception in the second line above.

E/Icon: Unable to load resource 0x7f020259 from pkg=
android.content.res.Resources$NotFoundException: Resource ID #0x7f020259
    at android.content.res.Resources.getValue(Resources.java:2558)
[...]

I think the problem is that the icon is from a different app and can't be accessed that easy, but i can't figure out how to access the icon correctly.

I know how to access resources from other apps when i have a resource id as int, but in this case i only have an icon but no resource id.

Edit:

Documentation: https://developer.android.com/reference/android/app/Notification.Action.html#getIcon()

Accessing the pending intent from the actions works perfectly

The Notification.Action.Icon.java doesn't store the icon bitmap, it only store its resource ID, your app can't read other app's resource, it searches its own resource and can't find the ID, so throws a exception.

in Notification.java

public Icon getIcon() {
    if (mIcon == null && icon != 0) {
        // you snuck an icon in here without using the builder; let's try to keep it
        mIcon = Icon.createWithResource("", icon);
    }
    return mIcon;
}

in Icon.java

public static Icon createWithResource(String resPackage, @DrawableRes int resId) {
    if (resPackage == null) {
        throw new IllegalArgumentException("Resource package name must not be null.");
    }
    final Icon rep = new Icon(TYPE_RESOURCE);
    rep.mInt1 = resId;     
    rep.mString1 = resPackage      
    return rep;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM