如何在Android的TextView上使用图像(设备上存储的图像)和文本?

11

我正在创建聊天应用程序,在其中从服务器获取表情符号(EMOJI)(图像URL)。

我通过以下代码将这些图像(表情符号URL)与文本一起在我的TextView中使用。

String stringWithHtml = "Sample string with an <img src=\"http://MY_SERVER.emoji.s3.amazonaws.com/cf68/5794d5f7895fa10a8f8e1350/imgList/5794d5f7895fa10a8f8e136a.png\"></img>" +
                        "<img src=\"http://MY_SERVER.emoji.s3.amazonaws.com/cf68/5794d5f7895fa10a8f8e1350/imgList/5794d5f7895fa10a8f8e135a.png\"></img>"+
                        "<img src=\"http://MY_SERVER.emoji.s3.amazonaws.com/cf68/5794d5f7895fa10a8f8e1350/imgList/5794d5f7895fa10a8f8e135b.png\"></img>"; 


Drawable drawable = Drawable.createFromStream(new URL(source).openStream(), "src name");
                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

Spanned spannedValue = Html.fromHtml(stringWithHtml, drawable, null); 
MY_TEXTVIEW.setText(spannedValue);

我在AsynTask中使用这些内容并获得了期望的结果,如下所示:

My Result

现在,我将所有表情符号(图片)存储在我的设备上,并想在我的TextView中将其与文本一起使用。

我的问题是:我们如何在TextView上使用设备(存储的图像)和文本?

我在SO上搜索过它,但没有获得预期的结果。请查看我访问过的以下链接

1. 第一个链接
2. 第二个链接
3. 第三个链接
4. 第四个链接

我已经使用了ImageSpan,但是出现了其他问题,我在SO上发布了问题在这里点击

请帮助我解决这个问题。谢谢


嗨,Ravindra Kushwaha,你可以查看我的答案。 - KeLiuyue
https://dev59.com/Nl8d5IYBdhLWcg3waxkp - vikas kumar
你尝试过这个解决方案吗?你之前关于ImageSpan的帖子格式非常糟糕 - 它甚至没有表达任何对ImageSpan的使用,所以很可能被忽略了。 - Dioxin
@VinceEmigh 请查看我的链接 https://stackoverflow.com/questions/45674410/getting-exceptionbitmapfactory-unable-to-decode-stream-android-while-using - Ravindra Kushwaha
4个回答

8

好的,你甚至不需要Uri。试试这个:

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/car_icon.png";

                String stringWithHtml = "Sample string with an <img src=" + path + "></img>"
                +" <img src=" + path + "></img>"
                +" <img src=" + path + "></img>";

                Html.ImageGetter getter = new Html.ImageGetter() {
                    @Override
                    public Drawable getDrawable(String s) {
                        return BitmapDrawable.createFromPath(s);
                    }
                };               

                Spanned spannedValue = Html.fromHtml(stringWithHtml, getter, null);
                text.setText(spannedValue);'

请问您能否在此基础上添加更多的代码吗?我没有理解您的解决方案。 - Ravindra Kushwaha
好的,我会尽快让你知道。 - Ravindra Kushwaha
我已经尝试了您的解决方案,但它没有起作用。请检查我的代码,我已按照您的建议进行了修改。https://ibb.co/caDk75 - Ravindra Kushwaha
请尝试我附加的链接中的解决方案。该链接中的解决方案百分之百有效。 - Dmitriy Mitiai
在链接中,他们没有使用设备图像,请检查一下。 - Ravindra Kushwaha
显示剩余2条评论

2
你可以实现一个Java程序来获取你接收到的表情符号数量。
并且使用下面的程序动态创建 UIView 中的 ImageView 组件。请使用循环结构重复尽可能多的次数。
ImageView iv = new ImageView();
RelativeLayout parentView = findViewById("R.id.parentViewId");
parentView.addView(iv, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

1
这是从服务器和SD卡加载图像到文本视图的代码.. 您可以使用SD卡相关的代码 :)
Spanned spanned = null;
String messageCustomized = "<img src ='"+ imageFileName +"'/>";
Spanned span = Html.fromHtml(messageCustomized, new 
URLImageParser(sentMessagesViewHolder.tvMessage, context), null);
if (spanned!=null) {
      spanned = (Spanned) TextUtils.concat(spanned, span);
}else spanned= span;

if (spanned!=null) {
   txtView.setText(spanned);
 }

ImageGetter

public class URLImageParser implements ImageGetter {
Context context;
View container;
private int imageSize = 20;
private int imageSizeDisplaySize = 20;
URLDrawable urlDrawable = null;

public URLImageParser(View container, Context context) {
    this.context = context;
    this.container = container;
    imageSize = Utility.convertDpTopPixels(context, 20);
    imageSizeDisplaySize = Utility.convertDpTopPixels(context, 35);

}

@Override
public Drawable getDrawable(final String fileName) {


    urlDrawable = new URLDrawable();
    Drawable drawable = null;
    if (Build.VERSION.SDK_INT >= 21)
        drawable = context.getDrawable(R.drawable.profile_main_placeholder);
    else
        drawable = context.getResources().getDrawable(R.drawable.profile_main_placeholder);

    drawable.setBounds(0, 0, 0 + imageSize, 0 + imageSize);
    urlDrawable.drawable = drawable;

    Bitmap bitmap = null;
    bitmap = ImageUtility.getImageFromSDCard(fileName);
    if (bitmap != null) {   // the bitmap is available

        bitmap = RoundedImageView.getCroppedBitmap(bitmap, imageSize, imageSize, imageSize);
        drawable = new BitmapDrawable(context.getResources(), bitmap);//ImageUtility.bitmapToDrawable(context,resource);
        drawable.setBounds(0, 0, 0 + imageSize, 0 + imageSize); //set the correct bound according to the result from HTTP call
        URLImageParser.this.urlDrawable.drawable = drawable;

    }
    return urlDrawable.drawable; 

}
}

URLDrawable (网址可绘制对象)
public class URLDrawable extends BitmapDrawable {
   protected Drawable drawable;

   @Override
   public void draw(Canvas canvas) {
    // override the draw to facilitate refresh function later
       if(drawable != null) {
           drawable.draw(canvas);
       }
   }
}

请查看URLImageParser。 - Naveed Ali
兄弟,请检查这一行 drawable = context.getDrawable(R.drawable.profile_main_placeholder);,它正在使用应用程序的图像...我有一张图片在我的SD卡上,我想将其与文本一起使用...请再次检查您的代码...谢谢。 - Ravindra Kushwaha
bitmap = ImageUtility.getImageFromSDCard(fileName);如果(bitmap != null) { // 位图可用 bitmap = RoundedImageView.getCroppedBitmap(bitmap, imageSize, imageSize, imageSize); drawable = new BitmapDrawable(context.getResources(), bitmap);//ImageUtility.bitmapToDrawable(context,resource); drawable.setBounds(0, 0, 0 + imageSize, 0 + imageSize); //根据HTTP调用的结果设置正确的边界 URLImageParser.this.urlDrawable.drawable = drawable; - Naveed Ali
让我们在聊天中继续这个讨论 - Naveed Ali
你明白了吗? - Naveed Ali
显示剩余7条评论

0

您可以使用emojicon库,也可以参考这里,示例源代码已包含。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:emojicon="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <io.github.rockerhieu.emojicon.EmojiconTextView
            android:id="@+id/txtEmojicon"
            android:text="I \ue32d emojicon"
            emojicon:emojiconAlignment="baseline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <io.github.rockerhieu.emojicon.EmojiconEditText
            android:id="@+id/editEmojicon"
            android:text="I \ue32d emojicon"
            emojicon:emojiconSize="28sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    <fragment
            android:id="@+id/emojicons"
            android:layout_width="match_parent"
            android:layout_height="220dp"
            class="io.github.rockerhieu.emojicon.EmojiconsFragment"/>
</LinearLayout>

你可以像下面这样添加自定义表情符号
 EmojiconsView emojiconsView = (EmojiconsView) findViewById(R.id.emojicons_view);
        emojiconsView.setPages(Arrays.asList(
                new EmojiconPage(Emojicon.TYPE_PEOPLE, null, false, R.drawable.ic_emoji_people_light),
                new EmojiconPage(Emojicon.TYPE_NATURE, null, false, R.drawable.ic_emoji_nature_light),
                new EmojiconPage(Emojicon.TYPE_OBJECTS, null, false, R.drawable.ic_emoji_objects_light),
                new EmojiconPage(Emojicon.TYPE_PLACES, null, false, R.drawable.ic_emoji_places_light),
                new EmojiconPage(Emojicon.TYPE_SYMBOLS, null, false, R.drawable.ic_emoji_symbols_light)
        ));
}

enter image description here


我没有使用任何表情符号。正如我在问题中提到的,我需要在TEXTVIEW上使用带有文本的图像。这些图像保存在我的设备上。我需要使用带有文本的图像。 - Ravindra Kushwaha
您分享的链接是带有表情符号的文本链接,不适用于带有图像的文本。 - Ravindra Kushwaha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接