共计 806 个字符,预计需要花费 3 分钟才能阅读完成。
html 代码块里面加载图片,
加载数据 ” <p> xxxxx 内容。</p><img src=”http://hg-test.com/lppt/weChat_official_account.jpg” />”
正常些加载会报异常,不能在主线程加载网络图片,解决方法
Drawable drawable; | |
Html.ImageGetter imageGetter = new Html.ImageGetter() { | |
@Override | |
public Drawable getDrawable(String s) {if (drawable != null) {return pic;} | |
else {getImage(s); | |
} | |
return null; | |
} | |
}; |
private void getImage(final String s) {new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try {final Drawable d = Drawable.createFromStream(new URL(s).openStream(), ""); | |
context.runOnUiThread(new Runnable() { | |
@Override | |
public void run() {if (d != null) { | |
// 必须设置 | |
d.setBounds(0,0,600,600); | |
drawable = d; | |
if (Build.VERSION.SDK_INT >= 24){textview.setText(Html.fromHtml(source,Html.FROM_HTML_MODE_COMPACT,imageGetter,null)); | |
}else{textview.setText(Html.fromHtml(source,imageGetter,null)); | |
} | |
} | |
} | |
}); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
} | |
}).start(); |
正文完