图像在通知行中显示为“[OBJ]”

时间:2017-06-12 10:17:53

标签: java android android-notifications android-drawable android-bitmap

我一直在尝试将drawable文件夹中的图片添加到通知行中。但图片未显示,而是显示[OBJ]

我可以在通知的任何其他地方添加图片。另外,我可以将emoji添加到通知行中,例如此问题Android notification - custom inboxstyle (add line )

但我无法添加图片。

有没有机会克服这个问题?

public void someMethod() {
    ...
    mInboxStyle.addLine(imageSpanned());
    ...
}


private Spanned imageSpanned () {
    String imgString = "Something <img src=\"ic_arrow_forward_black_24dp\">";
    return Html.fromHtml(imgString, mImageGetter, null);
}


Html.ImageGetter mImageGetter = new Html.ImageGetter() {
    @Override
    public Drawable getDrawable(String source) {
        Drawable drawable;
        Resources res = mContext.getResources();
        int drawableId = res.getIdentifier(source, "drawable", mContext.getPackageName());
        drawable = res.getDrawable(drawableId);
        int width = drawable.getIntrinsicWidth();   
        int height = drawable.getIntrinsicHeight(); 
        drawable.setBounds(0, 0, width, height);
        return drawable;
    }
};

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要在ic_arrow_forward_black_24dpdrawables中提供mipmap图标资源。

SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("Something").append(" ");
builder.setSpan(
       new ImageSpan( 
            getActivity(), 
            R.drawable.ic_arrow_forward_black_24dp), 
            builder.length() - 1,  
            builder.length(),  
           0
       ));

textView.setText(builder);