Android 异步ImageGetter无法正常工作
我尝试异步从html下载内联图像,遵循此线程中的信息: 我可以把图片下载下来。但是 结果是这样的: 这就是它的外观以及如果我不异步下载它们时的外观: 有没有办法解决这个问题? 先谢谢你 编辑: my URLImageParser的代码:Android 异步ImageGetter无法正常工作,android,html-parsing,android-asynctask,textview,Android,Html Parsing,Android Asynctask,Textview,我尝试异步从html下载内联图像,遵循此线程中的信息: 我可以把图片下载下来。但是 结果是这样的: 这就是它的外观以及如果我不异步下载它们时的外观: 有没有办法解决这个问题? 先谢谢你 编辑: my URLImageParser的代码: public URLImageParser( View t, Context c ) { this.c = c; this.container = t; } public Drawable getDrawable( String sourc
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();
}
}
我找到了解决这个问题的办法。 您必须保留对TextView实例的引用,并且在下载图像后,必须在onPostExecute方法中使用相同的文本调用setText:
@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());
}
这将迫使TextView重新布局其内容。我不知道您在问什么。IE在这里做什么?IE是正在下载到我的应用程序中的屏幕截图。我希望你能分辨出这两幅画的区别。问题是,当异步下载图片时,它们会混合在一起,而我要查找的是第二张图片上显示的内容。这与下载无关!你把它们放错地方了好吧。因此,实际内容并不相关,只是图像不同这一事实?如果您发布几行代码,可能会有所帮助。
@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());
}