11

I am creating the chat app , in which i am getting the EMOJI from the server (IMAGE URLS).

I am using this images(Emoji url) with text in my TextView by below lines of the code.

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

This all stuff , i am using in the AsynTask and getting the expected result like below:-

My Result

Now i am storing all the emojis(Images) on my device and want to use it with text in my TextView.

My question is that How can we use the device(Stored images) with text on my TextView ?

I have searched about it on SO but did not get the expected result.Please check below link which i have visited.

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

I have used the ImageSpanfor it but the other problem arises , which i have posted the question on SO Click here

Please help me to short out from this problem.Thanks

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
  • Hi Ravindra Kushwaha,you can check my answer. – KeLiuyue Aug 02 '17 at 07:47
  • https://stackoverflow.com/questions/26859800/is-there-any-way-to-insert-an-imagespan-in-a-textview-without-disrupting-the-tex – vikas kumar Aug 14 '17 at 18:33
  • Have you tried [this solution](https://stackoverflow.com/a/16769333/2398375)? Your previous post about `ImageSpan` is *horribly* formatted - it doesn't even express any use of `ImageSpan`, so it was probably ignored by many. – Vince Aug 15 '17 at 16:18
  • @VinceEmigh please check my link https://stackoverflow.com/questions/45674410/getting-exceptionbitmapfactory-unable-to-decode-stream-android-while-using – Ravindra Kushwaha Aug 16 '17 at 07:12

4 Answers4

8

OK. You even no need Uri. try this:

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);'
Dmitriy Mitiai
  • 1,112
  • 12
  • 15
2

You can implement a java program to get the count of the emoji's you receive.

And Dynamically create ImageView component in the UIView using the below program. Please use looping structures to repeat as much as you need.

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));
Thomas Easo
  • 3,457
  • 3
  • 21
  • 32
1

Here is the code for loading images in textview from servers as well as from sdcard.. you can use sdcard related code :)

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);
       }
   }
}
Naveed Ali
  • 2,609
  • 1
  • 22
  • 37
  • i do not want to use the `url` like in your code `WebConstant.IMAGE_BASE_URL` ..I have stored image on my sd card. i need to use this images of sd card with my text..Please help me on it – Ravindra Kushwaha Aug 28 '17 at 07:39
  • kindly look at the code it checks if image is available in the sdcard then load from there.. – Naveed Ali Aug 28 '17 at 07:41
  • Have a look at URLImageParser – Naveed Ali Aug 28 '17 at 07:42
  • Bro please check this line ` drawable = context.getDrawable(R.drawable.profile_main_placeholder); ` , it is using the app images..I have image on my sd card which i want to use these with text..Please check ur code once..Thanks – Ravindra Kushwaha Aug 28 '17 at 07:44
  • 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; – Naveed Ali Aug 28 '17 at 07:53
  • could u please add it on the your solution – Ravindra Kushwaha Aug 28 '17 at 07:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152998/discussion-between-naveed-ali-and-ravindra-kushwaha). – Naveed Ali Aug 28 '17 at 07:54
  • did you get it? – Naveed Ali Aug 28 '17 at 07:55
  • for your effort +1 from my side...Plz give me time , i am on the others task..Will let u know tomorrow – Ravindra Kushwaha Aug 28 '17 at 09:12
  • Ok I have updated code.. now it will be easy for you – Naveed Ali Aug 28 '17 at 12:04
  • please help me..What is `URLDrawable` , as i copied urs code ..It is not recognizing the `URLDrawable`..PLease check the image https://ibb.co/jiM6u5 – Ravindra Kushwaha Aug 29 '17 at 09:47
  • Can u please help me on the URL images ? – Ravindra Kushwaha Dec 06 '17 at 14:06
0

You can use emojicon library , also refer here , sample source code included.

<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>

you can add custom emojis like below

 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

Omar Dhanish
  • 885
  • 7
  • 18