2

Please don't mark it as duplicate as other post don't help me out for android above 4.2

I'm implementing a task in which I have to display HTML content into TextView everything is working fine except images. Somewhere text are overlapping with image I also go through many posts for this even on stackoverflow also however didn't get any success. I found a solution from This post and got better image however, still I'm getting same concern. For reference I'm attaching a screen-shot also. Have a look .

I'm getting this concern on some devices like: Nexus-5, Galaxy-S3 and all are having android version 4.2+

Following is my code:

public class URLImageParser implements ImageGetter 
{
    private Context oContext;
    private TextView container;
    private LayoutCustomization oLayoutCustomization;

/***
 * Construct the URLImageParser which will execute AsyncTask and refresh the container
 * @param oTextView
 * @param oContext
 */
public URLImageParser(TextView oTextView, Context oContext) 
{
    this.oContext = oContext;
    this.container = oTextView;
    oLayoutCustomization    = new LayoutCustomization(this.oContext.getResources().getDisplayMetrics());        
}

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<String, Void, Drawable>  
{
    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) 
    {
        if(result == null)
        {
            urlDrawable.drawable = oContext.getResources().getDrawable(R.drawable.ic_launcher);

            // redraw the image by invalidating the container
            URLImageParser.this.container.invalidate();
            return;
        }
        // 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();

     // For ICS
        URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() + result.getIntrinsicHeight()));

        // Pre ICS
        URLImageParser.this.container.setEllipsize(null);

        URLImageParser.this.container.setText(URLImageParser.this.container.getText());
    }

    /***
     * Get the Drawable from URL
     * @param urlString
     * @return
     */
    public Drawable fetchDrawable(String urlString) 
    {
        try 
        {
            URL aURL = new URL(urlString);
            final URLConnection conn = aURL.openConnection(); 
            conn.connect(); 
            final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); 
            final Bitmap bm = BitmapFactory.decodeStream(bis);
            @SuppressWarnings("deprecation")
            Drawable drawable = new BitmapDrawable(bm);
            drawable.setBounds(0,0,bm.getWidth(),bm.getHeight());
            return drawable;
        } 
        catch (Exception e) 
        {
            return null;
        } 
    }
}`

And

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

And this is how I implemented above code:

URLImageParser oImageParser = new URLImageParser(oTextView, oContext);
Spanned htmlSpan = Html.fromHtml("SOME_HTML_STRING", null);
oTextView.setText(htmlSpan);

Please help.

Community
  • 1
  • 1
Harish Godara
  • 2,388
  • 1
  • 14
  • 28

2 Answers2

1

Set the text again in onPostExecute.

Ex. txtTest.setText(txtTest.getText());

Marius
  • 15,148
  • 9
  • 56
  • 76
Parin Parikh
  • 385
  • 1
  • 6
0

Change the code below

Spanned htmlSpan = Html.fromHtml("SOME_HTML_STRING", null); 

to

Spanned htmlSpan = Html.fromHtml("SOME_HTML_STRING", oImageParser);
JohnWatsonDev
  • 1,227
  • 9
  • 16