好吧,我对这个失去了理智。我的程序中有一个解析HTML的方法。我想要包含内嵌图像,我认为使用Html.fromHtml(字符串,Html.ImageGetter,Html.TagHandler)将允许这种情况发生。
由于Html.ImageGetter没有实现,因此我可以编写一个实现。但是,由于将URL解析为Drawables需要网络访问,我不能在主线程上执行此操作,因此它必须是AsyncTask。我想。
但是,当您将ImageGetter作为参数传递给Html.fromHtml时,它会使用必须重写的getDrawable方法。因此无法调用触发doInBackground方法的整个ImageGetter.execute交易,因此无法实际实现此异步。
我是否完全错了,或者更糟糕的是,这是不可能的?感谢
答案 0 :(得分:99)
我做了一些非常类似的事情(我想),你想做什么。那时我需要做的是解析HTML并将其设置回TextView,我也需要使用Html.ImageGetter
并在主线程上获取图像时遇到同样的问题。
我基本上采取的步骤:
URLDrawable
getDrawable
方法返回Html.ImageGetter
onPostExecute
后,我会重绘Spanned
结果现在,URLDrawable的代码如下
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);
}
}
}
很简单,我只是覆盖draw
所以它会选择AsyncTask完成后我在那里设置的Drawable。
以下类是Html.ImageGetter
的实现,是从AsyncTask
获取图像并更新图像的实现
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<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) {
// 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();
}
}
}
最后,下面是演示工作原理的示例程序:
String html = "Hello " +
"<img src='http://www.gravatar.com/avatar/" +
"f9dd8b16d54f483f22c0b7a7e3d840f9?s=32&d=identicon&r=PG'/>" +
" This is a test " +
"<img src='http://www.gravatar.com/avatar/a9317e7f0a78bb10a980cadd9dd035c9?s=32&d=identicon&r=PG'/>";
this.textView = (TextView)this.findViewById(R.id.textview);
URLImageParser p = new URLImageParser(textView, this);
Spanned htmlSpan = Html.fromHtml(html, p, null);
textView.setText(htmlSpan);
答案 1 :(得分:5)
非常好。但是,不推荐使用 DefaultHttpClient 类型。在获取方法上试试这个:
private InputStream fetch(String urlString) throws MalformedURLException, IOException {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream stream = urlConnection.getInputStream();
return stream;
}
答案 2 :(得分:1)
我有点困惑,你想要呈现的HTML是静态的,仅用于格式化,还是动态的,来自网络?如果你想要后者,即渲染HTML并检索图像,那么它会有点痛苦(建议 - 只需使用WebView?)。
无论如何,您首先必须运行AsyncTask来检索初始HTML。然后,您将使用Html.fromHtml()
类的自定义实现将这些结果传递到Html.ImageGetter
。然后在该实现中,你必须启动一个单独的AsyncTask来检索每个图像(你可能想要实现一些缓存)。
然而,通过阅读文档(我认为我已经看过一些样本),在我看来,这不是他们所说的Html.ImageGetter
。我认为它适用于带有内部 drawables引用的硬编码HTML,但这只是我的看法。
答案 3 :(得分:1)
如果您使用Picasso,请将@momo代码的一部分更改为
cc
答案 4 :(得分:0)
AsyncTask task = new AsyncTask(){
@Override
protected String doInBackground(Integer... params) {
span = Html.fromHtml(noticeList.get(0)
.getContent(), imgGetter, null);
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
text.setMovementMethod(ScrollingMovementMethod
.getInstance());
if(span != null){
text.setText(span);
}
}
};
task.execute();