ImageSpan in a widget?
Asked Answered
L

1

0

Are there any limitations to adding an ImageSpan to a widget? This identical code works fine in a standard TextView.

SpannableStringBuilder buf = new SpannableStringBuilder("");
if(!TextUtils.isEmpty(message.getMessageBody())) {
    SmileyParser parser = SmileyParser.getInstance();
    buf.append(parser.addSmileySpans(group ? message.getMessageBodyWithoutName() : message.getMessageBody()));
}
view.setTextViewText(R.id.message_body, buf);

Thanks.

Edit 1:

public CharSequence addSmileySpans(CharSequence text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);

Matcher matcher = mPattern.matcher(text);
while (matcher.find()) {
    int resId = mSmileyToRes.get(matcher.group());
    builder.setSpan(new ImageSpan(mContext, resId),
                    matcher.start(), matcher.end(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return builder;

}

Lithea answered 30/9, 2011 at 21:49 Comment(2)
Where are the smileys coming from? IOW, what are you using for the Uri? And what are your symptoms?Cards
The smiley's are local assets. The parser returns an ImageSpan with the drawable set correctly (works in multiple other locations), just the widget fails to display and is using to text instead.Lithea
C
1

The smiley's are local assets.

I am going to interpret this literally, that you mean your images are in assets/.

My guess is that the home screen is having difficulty resolving your asset reference. As a test, try putting the images on external storage and using Uri.fromFile() to create your Uri. If that works, try putting them as drawable resources and using the resource IDs. Or, try the resource Uri syntax:

Uri.parse("android.resource://your.package.name.goes.here/" + R.raw.myvideo);
Cards answered 30/9, 2011 at 22:30 Comment(4)
I apologize, let me rephrase the statement. The images are stored in drawable-mdpi/hdpi and referenced by their resource id, when they are instantiated in new ImageSpan(). The widget has no problem displaying any other images I use for layout, content, its only when I use the ImageSpan in the TextView that it just doesn't work. I'll try your method and report back with the results.Lithea
@Tyler: My best guess is that the home screen, when interpreting an ImageSpan, does not realize that it needs to get it from your app's resources instead of its own. The android.resource Uri syntax may get you past this problem.Cards
Thanks for the recommendation, unfortunately using the android.resource uri you recommended didn't work. Before I invest much time in this fix, is this even feasible or is there a known limitation here?Lithea
@Tyler: If the android.resource Uri did not work, you're probably out of luck, unless you're willing to store the images on external storage or are willing to implement a tiny ContentProvider so you can use content:// Uri values (and then store the images in your getFilesDir() location, so the ContentProvider can serve them). I can't even guarantee that those will work, but they have decent odds. You are trying to get some foreign process to render your images, and off the cuff it would appear that ImageSpan didn't have that scenario in mind.Cards

© 2022 - 2024 — McMap. All rights reserved.