title: Images not loading in textview Html.fromHtml method
body: <p>I have created a simple textview to display html content. The text is showing up well, but the images are not showing up. I had referred <a href="https://stackoverflow.com/a/15617341/1627599">this</a> to download the images. But the result is the same. The images are replaced with boxes.<img src="https://i.stack.imgur.com/HrI9G.png" alt="enter image description here"></p>
  
  <p><strong>The Activity</strong>:</p>
  
  <pre><code>package com.example.loadinghtmlinlist;
  
  import java.util.HashMap;
  import java.util.List;
  
  import android.app.ActionBar.LayoutParams;
  import android.app.Activity;
  import android.graphics.Color;
  import android.os.Bundle;
  import android.text.Html;
  import android.text.Spanned;
  import android.util.Log;
  import android.view.Gravity;
  import android.widget.TableLayout;
  import android.widget.TableRow;
  import android.widget.TextView;
  
  import com.parse.FunctionCallback;
  import com.parse.ParseCloud;
  import com.parse.ParseObject;
  
  public class PostListActivity extends Activity {
      TableLayout select_city_table;
  
      @Override
      protected void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_post_list);
          // Find the ListView resource.   
          select_city_table = (TableLayout) findViewById(R.id.select_city_table);
          setStory();
          cityListBody("Hello");
      }
  
      public void setStory(){
  
          // Pull data from Parse
          HashMap&lt;String, Object&gt; params = new HashMap&lt;String, Object&gt;();
          params.put("userid", "xxxxxxxxx");
          params.put("skip", 0);
  
          ParseCloud.callFunctionInBackground("studentsPosts", params, new FunctionCallback&lt;List&lt;List&lt;ParseObject&gt;&gt;&gt;() {
              @Override
              public void done(List&lt;List&lt;ParseObject&gt;&gt; arg0, com.parse.ParseException arg1){
                  if (arg0 == null) {
  
                  } else {
                      Log.e("size ", "RUNNING Size :   " + arg0.size());
                      if (arg0.size() == 0) {
                          Log.e("size is zero", "RUNNING BOOLEAN SOMETHING");
  
                      }
  
                      if (arg0.size() &gt; 0) {
  
                      }
                      if (arg0.size() &gt; 0) {
                          for (int i = 0; i &lt; arg0.size(); i++) {
                              if(arg0.get(i).get(0).get("htmlContent") != null){
                                 // INSERTOBJECTOGETDATA.GETDATA(arg0.get(i).get(0).getString("htmlContent"));
                                  //Toast.makeText(PostListActivity.this, arg0.get(i).get(0).getString("htmlContent"), Toast.LENGTH_SHORT).show();
                                  cityListBody(arg0.get(i).get(0).getString("htmlContent"));
  
                                 }
  
                          }   
                      }
                  }
  
              }
  
          });
  
      }
  
      // method to populate city body
          public void cityListBody(String strcity_name){
              // ----------------Select city
              // body------------------------------------------
              TableRow city_list_tr_data;
              city_list_tr_data = new TableRow(this);
              city_list_tr_data.setId(10);
              // city_list_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
              city_list_tr_data.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  
              final TextView city_name = new TextView(this);
              city_name.setId(21);// define id that must be unique
              // no_of_types.setText(parser.getValue(e, KEY_RIGHTMARKS)); // set
              // the text for the header
  
              URLImageParser p = new URLImageParser(city_name, this);
              Spanned htmlSpan = Html.fromHtml(strcity_name, p, null);
              city_name.setText(htmlSpan);
  
              city_name.setText(Html.fromHtml(strcity_name));
              city_name.setTextColor(Color.BLACK); // set the color
              city_name.setPadding(5, 5, 5, 5); // set the padding (if required)
              city_name.setGravity(Gravity.CENTER);
              city_name.setTextSize(16);
              city_list_tr_data.addView(city_name); // add the column to
              // the table row
              // here
  
  
  
              select_city_table.addView(city_list_tr_data, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  
  
  
          }
  
  
  }
  </code></pre>
  
  <p><strong>The URLImageParser class</strong>:</p>
  
  <pre><code>package com.example.loadinghtmlinlist;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.MalformedURLException;
  
  import org.apache.http.HttpResponse;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.impl.client.DefaultHttpClient;
  
  import android.content.Context;
  import android.graphics.drawable.Drawable;
  import android.os.AsyncTask;
  import android.text.Html.ImageGetter;
  import android.view.View;
  
  
  
  public class URLImageParser implements ImageGetter {
      Context c;
      View container;
  
      /***
       * Construct the URLImageParser which will execute AsyncTask and refresh the container
       * @param t
       * @param c
       */
      public URLImageParser(View t, Context c) {
          this.c = c;
          this.container = t;
      }
  
      public Drawable getDrawable(String source) {
          URLDrawable urlDrawable = new URLDrawable();
  
          // get the actual source
          ImageGetterAsyncTask asyncTask = 
              new ImageGetterAsyncTask( urlDrawable);
  
          asyncTask.execute(source);
  
          // return reference to URLDrawable where I will change with actual image from
          // the src tag
          return urlDrawable;
      }
  
      public class ImageGetterAsyncTask extends AsyncTask&lt;String, Void, Drawable&gt;  {
          URLDrawable urlDrawable;
  
          public ImageGetterAsyncTask(URLDrawable d) {
              this.urlDrawable = d;
          }
  
          @Override
          protected Drawable doInBackground(String... params) {
              String source = params[0];
              return fetchDrawable(source);
          }
  
          @Override
          protected void onPostExecute(Drawable result) {
              // set the correct bound according to the result from HTTP call
              urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0 
                      + result.getIntrinsicHeight()); 
  
              // change the reference of the current drawable to the result
              // from the HTTP call
              urlDrawable.drawable = result;
  
              // redraw the image by invalidating the container
              URLImageParser.this.container.invalidate();
          }
  
          /***
           * Get the Drawable from URL
           * @param urlString
           * @return
           */
          public Drawable fetchDrawable(String urlString) {
              try {
                  InputStream is = fetch(urlString);
                  Drawable drawable = Drawable.createFromStream(is, "src");
                  drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 
                          + drawable.getIntrinsicHeight()); 
                  return drawable;
              } catch (Exception e) {
                  return null;
              } 
          }
  
          private InputStream fetch(String urlString) throws MalformedURLException, IOException {
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpGet request = new HttpGet(urlString);
              HttpResponse response = httpClient.execute(request);
              return response.getEntity().getContent();
          }
      }
  }
  </code></pre>
  
  <p><strong>The URLDrawable class</strong>:</p>
  
  <pre><code>package com.example.loadinghtmlinlist;
  
  import android.graphics.Canvas;
  import android.graphics.drawable.BitmapDrawable;
  import android.graphics.drawable.Drawable;
  
  public class URLDrawable extends BitmapDrawable {
      // the drawable that you need to set, you could set the initial drawing
      // with the loading image if you need to
      protected Drawable drawable;
  
      @Override
      public void draw(Canvas canvas) {
          // override the draw to facilitate refresh function later
          if(drawable != null) {
              drawable.draw(canvas);
          }
      }
  }
  </code></pre>
  
  <p>Where am I going wrong? What needs to be altered to show the images properly?</p>
  
  <p>After applying Haresh Chhelana's suggestion, the output displays image but the output is malformed:
  <img src="https://i.stack.imgur.com/vgosU.png" alt="enter image description here"></p>