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.

1
  • if its working then vote for the answer Commented Sep 20, 2012 at 12:23

3 Answers 3

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);
}
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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));

3 Comments

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
@SquiredSquire I tried your code but it given error The method setImageResource(int) in the type ImageView is not applicable for the arguments (String).
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.