1

I wan to display an image in textview. I have an image in res/drawable/img.png Here is my code:

String img="<img src='file:///res/drawable/img.png' />";
txt_html.append(Html.fromHtml(img)); 

But it's not work.

Any idea?

ollo
  • 24,797
  • 14
  • 106
  • 155
Han Tran
  • 2,073
  • 4
  • 22
  • 37
  • possible duplicate of http://stackoverflow.com/questions/2865452/is-it-possible-to-display-inline-images-from-html-in-an-android-textview – Chris Nov 13 '11 at 15:08
  • Duplicate of: http://stackoverflow.com/questions/5561981/how-to-display-image-in-androids-textview – SystemParadox Nov 13 '11 at 15:08
  • @Chris: Thank for link, I'll read it – Han Tran Nov 13 '11 at 15:17
  • @SystemParadox: Your link not work :| I read all the topic you guys give, but It seen too hard to solve problem – Han Tran Nov 13 '11 at 15:20
  • @HanTran: if you're finding it difficult getting your head around the two solutions given above, you might want to try using a WebView instead of a TextView. – Teasel Nov 13 '11 at 23:40

2 Answers2

4
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.widget.TextView;

public class TextImageActivity extends Activity {
    int imageNumber = 1; //int to check which image is displayed
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tvText = (TextView) findViewById(R.id.text);
    final String testContent = "<html><body><b>Test</b><i>Italic</i><br/>"
        + "<img src=\"icon.png\"/>This is like testing if this thing works" + "<img src=\"a.png\"/>" +
                " in a more elaborate</body></html>";
    tvText.setText(Html.fromHtml(testContent, imgGetter, null));
}

  private ImageGetter imgGetter = new ImageGetter() {

    public Drawable getDrawable(String source) {
        Drawable drawable = null;
        if(imageNumber == 1) {
        drawable = getResources().getDrawable(R.raw.icon);
        ++imageNumber;
        } else drawable = getResources().getDrawable(R.raw.a);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
                                .getIntrinsicHeight());

        return drawable;
    }
 };

 }
waqas
  • 41
  • 2
3

To download images from the web and displaying in textView I used this:

public class MainActivity extends ActionBarActivity {

public String htmlContent;
int ScreenW,ScreenH;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_news);

    this.context = getBaseContext();

    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    float dpWidth = displayMetrics.widthPixels;
    float dpHeight = displayMetrics.heightPixels;

    ScreenW = (int) dpWidth;
    ScreenH = (int) dpHeight;

    try {

        ((TextView) findViewById(R.id.tv_my_content)).setText(Html.fromHtml(htmlContent, Images, null));
    }
    catch (Exception ex)
    {
        
    }
}

private Html.ImageGetter Images = new Html.ImageGetter() {

    public Drawable getDrawable(String source) {
        
        Drawable drawable = null;

        FetchImageUrl fiu = new FetchImageUrl(context,source);
        try {
            fiu.execute().get();
            drawable = fiu.GetImage();
        }
        catch (Exception e) {
            drawable = getResources().getDrawable(R.drawable.img_news);
        }
         // to display image,center of screen
        int imgH = drawable.getIntrinsicHeight();
        int imgW = drawable.getIntrinsicWidth();
        int padding =20;
        int realWidth = ScreenW-(2*padding);
        int realHeight = imgH * realWidth/imgW;
        drawable.setBounds(padding,0,realWidth ,realHeight);
        return drawable;
    }
};
}

And The FetchImageUrl class is:

public class FetchImageUrl extends AsyncTask<String, String, Boolean> {


String imageUrl;
Context context;
protected Drawable image;

public FetchImageUrl(Context context, String url)
{
    this.imageUrl = url;
    image = null;
    this.context = context;
}

public Drawable GetImage()
{
    return image;
}


@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected Boolean doInBackground(String... args) {
    try {
        InputStream input_stream = (InputStream) new URL(imageUrl).getContent();
        image = Drawable.createFromStream(input_stream, "src name");
        return true;

    }
    catch (Exception e)
    {
        image = null;
    }
    return false;
}


@Override
protected void onPostExecute(Boolean result) {

}}
Community
  • 1
  • 1
Hamid
  • 1,493
  • 2
  • 18
  • 32
  • 1
    this one working and set image center and custom height & widht final float scale = getResources().getDisplayMetrics().density; int dpWidthInPx = (int) (ScreenW * scale); int dpHeightInPx = (int) (200 * scale); – Ramesh Prajapati Apr 16 '17 at 04:31