异步ImageGetter不正常 [英] Async ImageGetter not functioning properly

查看:158
本文介绍了异步ImageGetter不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从异步下载HTML内嵌图片,下面在这个线程的信息:的Andr​​oid HTML ImageGetter为AsyncTask的

我可以使图像下载好了。但是!

这是如何结束:

这是它应该如何看它的外观,如果我不异步下载它们:

任何想法,这可怎么解决吗?
谢谢你在前进。

编辑:

code我URLImageParser:

 公共URLImageParser(查看T,上下文C)
{
    this.c = C;
    this.container = T;
}公众可绘制getDrawable(字符串源)
{
    URLDrawable urlDrawable =新URLDrawable();    ImageGetterAsyncTask的AsyncTask =新ImageGetterAsyncTask(urlDrawable);    asyncTask.execute(源);    返回urlDrawable;
}公共类ImageGetterAsyncTask扩展的AsyncTask<弦乐,太虚,可绘制>
{
    URLDrawable urlDrawable;    公共ImageGetterAsyncTask(URLDrawable D)
    {
        this.urlDrawable = D;
    }    @覆盖
    保护可绘制doInBackground(字符串... PARAMS)
    {
        字符串源=参数[0];
        返回fetchDrawable(源);
    }    @覆盖
    保护无效onPostExecute(可绘制结果)
    {
        urlDrawable.setBounds(0,0,0 + result.getIntrinsicWidth(),0
                + result.getIntrinsicHeight());        urlDrawable.drawable =结果;        URLImageParser.this.container.invalidate();
    }    公众可绘制fetchDrawable(字符串urlString)
    {
        尝试{
            InputStream为=取(urlString);
            可绘制可绘制= Drawable.createFromStream(是,SRC);
            drawable.setBounds(0,0,0 + drawable.getIntrinsicWidth(),0
                    + drawable.getIntrinsicHeight());
            返回绘制;
        }
        赶上(例外五)
        {
            返回null;
        }
    }    私人的InputStream获取(字符串urlString)抛出异常
    {
        DefaultHttpClient的HttpClient =新DefaultHttpClient();
        HTTPGET请求=新HTTPGET(urlString);
        HTT presponse响应= httpClient.execute(请求);
        返回response.getEntity()的getContent()。
    }
}

而code为URLDrawable

  @覆盖
公共无效画(油画画布)
{
    如果(绘!= NULL)
    {
        drawable.draw(画布);
    }
}


解决方案

我发现了这个问题的解决方法。
你必须保持到TextView的实例的引用,图像被下载后,你有你的onPostExecute方法调用的setText具有相同的文字:

  @覆盖
保护无效onPostExecute(可绘制结果)
{
    urlDrawable.setBounds(0,0,0 + result.getIntrinsicWidth(),0
            + result.getIntrinsicHeight());    urlDrawable.drawable =结果;    // mTextView是参考TextView的实例
    mTextView.setText(mTextView.getText());
}

这迫使TextView的重新布局其内容。

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:

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

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

解决方案

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.

这篇关于异步ImageGetter不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆