我正在尝试使用图像转换 HTML 文本Html.fromHtml
并将其设置在 ListView 中,但图像显示为正方形!我已经实现了一个 imageGetter 并且我设置了
StrictMode.ThreadPolicy 策略 = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
避免android.os.NetworkOnMainThreadException
这是我的代码:
private class MyTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0) {
try {
URL rssUrl = new URL(link);
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (myRssFeed!=null)
{
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
for (RSSItem item : myRssFeed.getList()) {
Spanned span = Html.fromHtml(item.getDescription().toString(), getImageHTML(), null);
Map<String, String> datum = new HashMap<String, String>(0);
datum.put("title", item.getTitle());
datum.put("desc", span.toString());
data.add(datum);
}
SimpleAdapter adapter2 = new SimpleAdapter(getApplicationContext(), data, R.layout.listview, new String[] {"title", "desc"}, new int[] {R.id.text1,
R.id.text2});
setListAdapter(adapter2);
这是 ImageGetter:
public ImageGetter getImageHTML(){
ImageGetter ig = new ImageGetter(){
public Drawable getDrawable(String source) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
try{
Drawable d = Drawable.createFromStream(new URL(source).openStream(), "src name");
d.setBounds(0, 0, d.getIntrinsicWidth(),d.getIntrinsicHeight());
return d;
}catch(IOException e){
Log.v("IOException",e.getMessage());
return null;
}
}
};
return ig;
}
有人能帮我吗?