I am parsing text and images in listview in android app. I have successfully parsed text in listview but failed to parse images. I m also getting url of images such as url/image.jpeg but donot know how to get images in imageview from that url. Code is:
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
bitmap = DownloadImage(XMLfunctions.getValue(e, "thumb"));
image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(bitmap);
Log.e("imageView","image is:"+image);
//map.put("id", XMLfunctions.getValue(e, "id"));
map.put("title", XMLfunctions.getValue(e, "title"));
map.put("author", XMLfunctions.getValue(e, "author"));
map.put("catagory",XMLfunctions.getValue(e, "catagory"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.mainview,
new String[] {"title","author", "catagory" },
new int[] {R.id.title, R.id.author,R.id.catagory });
setListAdapter(adapter);
The method DownloadImage is:
private Bitmap DownloadImage(String URL)
{
Log.v("url",URL);
Bitmap bitmap = null;
InputStream in = null;
try {
in = openHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
Log.e("image","image"+bitmap);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
private InputStream openHttpConnection(String urlStr) {
InputStream in = null;
int resCode = -1;
try {
URL url = new URL(urlStr);
URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException ("URL is not an Http URL");
}
HttpURLConnection httpConn = (HttpURLConnection)urlConn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
resCode = httpConn.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return in;
}
I have searched from google and tried different ways but can't parse images. What i am making mistake? Any help will be more appreciated.