简体   繁体   中英

Show image in textview using Html.fromhtml();

I have a list adapter to show content frm my social network, i need to put inside the text the img tags, i've tried using Html.fromhtml and it's formatting the text but instead of shows the img it shows a gray square.

How can i achieve that? I'v read about ImageGetter but i still don't have it very clear.

Thanks

You can draw an Image in TextView using the Html img tag with Html.ImageGetter . But make sure your image is available in resource drawable folder

Here is a sample , The image will be loaded from the resource.

String htmlText = "Hai <img src=\"ic_launcher\"> Hello";

textView.setText(Html.fromHtml(htmlText, new Html.ImageGetter() {

@Override
public Drawable getDrawable(String source) {
   int resourceId = getResources().getIdentifier(source, "drawable",getPackageName());
   Drawable drawable = getResources().getDrawable(resourceId);
   drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            return drawable;
        }
    }, null));
 String mIsi = (String) bundle.get("isi");
        isi.setText(Html.fromHtml(mIsi, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(final String source) {
                Picasso.get()
                        .load(source).into(imageView, new Callback() {
                    @Override
                    public void onSuccess() {
                        drawable = imageView.getDrawable();

                        drawable.setBounds(0, 0,
                                drawable.getIntrinsicWidth(),
                                drawable.getIntrinsicHeight());
                    }

                    @Override
                    public void onError(Exception e) {
                        Log.v("test pic","err"+e.getMessage());
                    }
                });

                imageView.setVisibility(View.GONE);

                return drawable;
            }
        }, null));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM