AndroidTV get notification icon

huangapple go评论64阅读模式
英文:

AndroidTV get notification icon

问题

我正在尝试获取 Android TV 上活动通知的图标。我需要将其编码为 base64 格式并发送到 React Native。

该图标存储为 blob 类型,解码后如下所示:
android.graphics.drawable.Icon com.android.systemui/

这似乎只是一个指向图标的路径,但我需要获取真正的图标并将其编码为 base64。

我以以下方式检索数据:

private static final Uri NOTIF_CONTENT_URI = Uri.parse(
            "content://com.android.tv.notifications.NotificationContentProvider/" +
                    "notifications");

final String[] projection = { TITLE, TEXT, SMALL_ICON};

Cursor c = _reactContext.getContentResolver().query(NOTIF_CONTENT_URI, projection, null,
                null, null);

if (c != null && c.moveToFirst()) {
     do {
         String title = c.getString(c.getColumnIndex(TITLE));
         String text = c.getString(c.getColumnIndex(TEXT));
         byte[] smallIconBytes = c.getBlob(c.getColumnIndex(SMALL_ICON));
     } while (c.moveToNext());
}

获取标题和文本的操作正常运行,但将图标字节编码为 base64 后返回的是:

HgAAAGEAbgBkAHIAbwBpAGQALgBnAHIAYQBwAGgAaQBjAHMALgBkAHIAYQB3AGEAYgBsAGUALgBJAGMAbwBuAAAAAAACAAAAFAAAAGMAbwBtAC4AYQBuAGQAcgBvAGkAZAAuAHMAeQBzAHQAZQBtAHUAaQAAAAAALwUIAQAAAAAFAAAA

这可以解码为:

android.graphics.drawable.Icon com.android.systemui/

我该如何获取图标而不仅仅是路径?谢谢。

英文:

Im trying to get the icon of active notification on AndroidTV. I need to encode it to base64 and send it to react native.

The icon is stored as blob type and decoded looks like this

android.graphics.drawable.Icon  com.android.systemui/

which looks like just a path to the icon, but i would need to get the real icon and encode it to base 64.

Im retrieving data like this:

private static final Uri NOTIF_CONTENT_URI = Uri.parse(
            "content://com.android.tv.notifications.NotificationContentProvider/" +
                    "notifications");

final String[] projection = { TITLE, TEXT, SMALL_ICON};

Cursor c = _reactContext.getContentResolver().query(NOTIF_CONTENT_URI, projection, null,
                null, null);

if (c != null && c.moveToFirst()) {
     do {
         String title = c.getString(c.getColumnIndex(TITLE));
         String text = c.getString(c.getColumnIndex(TEXT));
         byte[] smallIconBytes = c.getBlob(c.getColumnIndex(SMALL_ICON));
     } while (c.moveToNext());
}

Getting title and text works fine, but encoding the icon bytes to base64 returns

HgAAAGEAbgBkAHIAbwBpAGQALgBnAHIAYQBwAGgAaQBjAHMALgBkAHIAYQB3AGEAYgBsAGUALgBJAGMAbwBuAAAAAAACAAAAFAAAAGMAbwBtAC4AYQBuAGQAcgBvAGkAZAAuAHMAeQBzAHQAZQBtAHUAaQAAAAAALwUIAQAAAAAFAAAA

which can be decoded to

android.graphics.drawable.Icon  com.android.systemui/

How can I get that icon and not just the path? Thank you.

答案1

得分: 0

以下是您要的翻译内容:

private static Icon getIconFromBytes(byte[] blob) {
        Parcel in = Parcel.obtain();
        Icon icon = null;
        if (blob != null) {
            in.unmarshall(blob, 0, blob.length);
            in.setDataPosition(0);
            icon = in.readParcelable(Icon.class.getClassLoader());
        }

        in.recycle();
        return icon;
}
英文:
private static Icon getIconFromBytes(byte[] blob) {
        Parcel in = Parcel.obtain();
        Icon icon = null;
        if (blob != null) {
            in.unmarshall(blob, 0, blob.length);
            in.setDataPosition(0);
            icon = in.readParcelable(Icon.class.getClassLoader());
        }

        in.recycle();
        return icon;
}

huangapple
  • 本文由 发表于 2020年8月27日 03:34:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63604601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定