0

I was trying to download inline images from html asynchronously, following the information in this thread: Android HTML ImageGetter as AsyncTask

I could make the images download okay. BUT!

This is how it ends up:

enter image description here

This is how it is supposed to look and how it looks if I don't download them asynchronously:

enter image description here

Any ideas to how this can be fixed? Thank you in advance.

EDIT:

Code for my URLImageParser:

public URLImageParser( View t, Context c )
{
    this.c = c;
    this.container = t;
}

public Drawable getDrawable( String source )
{
    URLDrawable urlDrawable = new URLDrawable();

    ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask( urlDrawable );

    asyncTask.execute( source );

    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 )
    {
        urlDrawable.setBounds( 0, 0, 0 + result.getIntrinsicWidth(), 0
                + result.getIntrinsicHeight() );

        urlDrawable.drawable = result;

        URLImageParser.this.container.invalidate();
    }

    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 Exception
    {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet( urlString );
        HttpResponse response = httpClient.execute( request );
        return response.getEntity().getContent();
    }
}

And the code for URLDrawable

@Override
public void draw( Canvas canvas )
{
    if( drawable != null )
    {
        drawable.draw( canvas );
    }
}
Community
  • 1
  • 1
Nicholas Magnussen
  • 769
  • 2
  • 9
  • 26
  • i have no idea what you are asking. what is IE doing here? – njzk2 Oct 12 '11 at 12:11
  • IE is screenshots that are being downloaded into my application. I hope you can tell the difference between the two pictures. The problem is that when downloading the pictures asynchronously, they get all mashed together, while what I'm looking for is what is displayed on the second picture. – Nicholas Magnussen Oct 12 '11 at 12:13
  • this has nothing to do with the downloads! You are layouting them in a wrong way – Sherif elKhatib Oct 12 '11 at 12:16
  • ok. so the actual content is not relevant, only the fact that the images are different? – njzk2 Oct 12 '11 at 12:18
  • it may help if you post a few lines of code. – njzk2 Oct 12 '11 at 12:18
  • We agree on that part. The thing is, that it is one big textView, since it's all one html response from an API, making it significantly harder to adjust. – Nicholas Magnussen Oct 12 '11 at 12:18
  • @njzk2 Code is now added. It was linked from the other post. Sorry for not making it clearer :) – Nicholas Magnussen Oct 12 '11 at 12:21
  • So, you have 2 images, but you only get to display the second one because of the async task, right? Do you use some sort of Adapter of any kind? Do you recycle views? It seems to me that your second image overwrite the first one the screen, which tends to indicate that your are using the same view at some point – njzk2 Oct 12 '11 at 12:28
  • It doesn't overwrite. It's writing on top of it. – Nicholas Magnussen Oct 12 '11 at 12:33

1 Answers1

1

I've found a workaround for this problem. You have to keep a reference to the TextView instance and after the image is being downloaded you have to call setText with the same text in your onPostExecute method:

@Override
protected void onPostExecute( Drawable result )
{
    urlDrawable.setBounds( 0, 0, 0 + result.getIntrinsicWidth(), 0
            + result.getIntrinsicHeight() );

    urlDrawable.drawable = result;

    // mTextView is the reference to the TextView instance
    mTextView.setText(mTextView.getText());
}

This forces the TextView to re-layout its content.

AZ13
  • 14,397
  • 5
  • 35
  • 31