0

I want to display image in list view where images are stores in drawble folder and i trying to read through XML here is my XML code.

<music>
<song>
    <title>Live Shows</title>
    <thumb_url>@drawable/chat.png</thumb_url>
</song>
<song>
    <title>Space Bound</title>
    <thumb_url>@drawable/email_open.png</thumb_url>
</song>

Please help me i am new to android.

Nishit Patel
  • 149
  • 1
  • 2
  • 13

3 Answers3

1

Parse xml and call the function given below

// imagePath - value of <thumb_url>@drawable/chat.png</thumb_url>
// imageView - view where image needs to be set
private void setImage(String imagePath,ImageView imageView)
{  

 String uriFromXML = imagePath;
 String suffix = ".png";
if ((uriFromXML != null) && (uriFromXML.startsWith("@")) && (uriFromXML.endsWith(suffix))) 
{
  int length = uriFromXML.length();
  int suffixLength = suffix.length();
  String uri = uriFromXML.substring(1, length - suffixLength);



  int imageResource = getResources().getIdentifier(uri, null,getPackageName());

  Drawable image = getResources().getDrawable(imageResource);
  imageView.setImageDrawable(image);
}
}
Hitesh Jain
  • 408
  • 2
  • 5
0

Okay, I presume you are using the androidhive tut. In the lazy adapter class change this code imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image); to thumb_image.setImageResource(song.get(CustomizedListView.KEY_THUMB_URL));

SquiresSquire
  • 2,404
  • 4
  • 23
  • 39
  • okay but can you give the example or tell me how to give path of image in XML so i will get the image directly in the image view – Nishit Patel Sep 18 '12 at 03:50
  • @SquiredSquire I tried your code but it given error `The method setImageResource(int) in the type ImageView is not applicable for the arguments (String)`. – Nishit Patel Sep 18 '12 at 07:36
  • @NishitPatel change the xml so it is displayed as file:///android_asset/chat.png and change to line to imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image); and move all images to assets folder – SquiresSquire Sep 18 '12 at 08:44
0

Try this, in Xml

<music>
<song>
    <title>Live Shows</title>
    <thumb_url>chat</thumb_url>
</song>
<song>
    <title>Space Bound</title>
    <thumb_url>email_open</thumb_url>
</song>

and in activity,

int redId = getResources().getIdentifier("<your_package>:drawable/<resource_name>", null, null);
imageView.setBackgroundDrawable(getResources().getDrawable(resId));

where would be something like com.example.package and <resource_name> would be filled by thumb_url from your xml.

References: Ref1 Ref2

Community
  • 1
  • 1
vikas
  • 1,535
  • 1
  • 13
  • 22