I'm trying to convert an HTML text with images using Html.fromHtml
and set it inside a ListView, but images appears as squares! I have implemented an imageGetter and also I set
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
to avoid android.os.NetworkOnMainThreadException
Here's my code:
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);
And here's the 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;
}
Can someone help me?
I'm not sure weather myRssFeed.getList() or item.getDescription() performes any network operation (Just guessing only), Just try adding your for loop logic inside doInBackground method.
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);
}
adapter2 = new SimpleAdapter(getApplicationContext(), data, R.layout.listview, new String[] {"title", "desc"}, new int[] {R.id.text1,
R.id.text2});
Declare SimpleAdapter adapter2 as a global variable in MyTask and move above code snippet to doInBackground method
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.