0

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.

Larik
  • 99
  • 3
  • 10

3 Answers3

1

As you haven't implemented any background image loading in your code, i suggest you to go through Lazy Loading images in ListView which i think mostly useful in your case. If you don't implement any background image fetching threading process then the screen will get hang and released again to actual mode when whole data is fetched.

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

You should probably be using BitmapFactory.decodeStream(inputstream);

so this returns you a bitmap so that now you can set it on an imageview.

hope it helps you.

cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
0

In my past experiences, this post from Android Developer Blog helped me much, especially with multithreading and performance issues: Multithreading for performance.

Rainbowbreeze
  • 1,503
  • 1
  • 9
  • 14