【问题标题】:How to use the Image(Stored Image of device) with Text on TextView Android?如何在 TextView Android 上使用带有文本的图像(设备的存储图像)?
【发布时间】:2017-08-30 01:45:43
【问题描述】:

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

我正在通过下面的代码行在我的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 中使用并获得如下预期结果:-

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

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

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

1. First Link
2. Second Link
3. Third Link
4. Forth Link

我已经使用了ImageSpan,但出现了另一个问题,我已将问题发布在SO Click here

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

【问题讨论】:

标签: java android image imagespan


【解决方案1】:

好的。你甚至不需要 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);'

【讨论】:

  • 你能在上面添加更多代码吗..我没有得到你的解决方案
  • 我编辑了我的答案
  • 好的,我会尽快通知您
  • 我已经尝试了我们的解决方案,但它不起作用..请检查我的代码,我已经按照你的要求完成了 ibb.co/caDk75
  • 尝试我附加的链接中的解决方案。来自链接的解决方案 100 %
【解决方案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));

【讨论】:

    【解决方案3】:

    这是从服务器和 sdcard 加载 textview 中图像的代码。你可以使用 sdcard 相关代码:)

    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);
           }
       }
    }
    

    【讨论】:

    • 我不想在您的代码WebConstant.IMAGE_BASE_URL 中使用url ..我已将图像存储在我的SD 卡上。我需要在我的文字中使用这张 sd 卡的图像..请帮帮我
    • 请查看它检查图像是否在 sdcard 中可用的代码,然后从那里加载..
    • 看看 URLImageParser
    • 兄弟请检查这一行` drawable = context.getDrawable(R.drawable.profile_main_placeholder); ` ,它正在使用应用程序图像..我的 SD 卡上有图像,我想将这些图像与文本一起使用..请检查您的代码一次..谢谢
    • 位图 = ImageUtility.getImageFromSDCard(fileName); if (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;
    【解决方案4】:

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

    <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)
            ));
    }
    

    【讨论】:

    • 我没有使用任何 EMOJI..正如我在问题中提到的那样,我需要在 TEXTVIEW 上带有文本的图像..图像保存在我的设备上..我需要使用带有文本的图像
    • 你分享的链接是带文字的表情符号。不适用于带图片的文字
    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多