Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to show images from resources drawable?

I'm not able to show an image which is saved in res/drawable folder. I use ImageGetter to do this. The code is below:

ImageGetter imageGetter3 = new ImageGetter() {                
    public Drawable getDrawable(String source) { 
        int id=0; 
        if (source.equals("smiley")) { 
            id = R.drawable.smiley; 
        } 
        Drawable d = getResources().getDrawable(id); 
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
        return d; 
    } 
};

directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n" 
           + "Sta met je rug voor de deur\n" 
           + Html.fromHtml("<img src=\"smiley\">", imageGetter3, null) + " Draai naar links\n";

What I see on the screen when running is a little square with "obj" text on it. So what is wrong? The image cannot be read or something else? How to show images?

Honestly I have Googled a lot and tried other methods of ImageGetter as well, but none of them seems to work, I tried these too, they don't work:

ImageGetter imageGetter2 = new ImageGetter() { 
 public Drawable getDrawable(String source) { 
      Drawable d = null; 
      d = Drawable.createFromPath(source); 
      d.setBounds(0,0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
      return d; 
 } 
};

ImageGetter imageGetter = new ImageGetter() {                
 public Drawable getDrawable(String source) { 
     Drawable drawFromPath; 
     int path = Route.this.getResources().getIdentifier(source, "drawable","com.prj56.tracingapp"); 
     drawFromPath = (Drawable) Route.this.getResources().getDrawable(path); 
     drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(), drawFromPath.getIntrinsicHeight()); 
     return drawFromPath; 
 } 
};  

========================================================= if (....) { ImageView iv1 = new ImageView(this); iv1.setImageResource(R.drawable.smiley);

    directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n" 
       + "Sta met je rug voor de deur\n";
       HERE COMES THE IMAGE! BUT HOW TO DO THIS? It's within directions textview...
    directions += " Draai naar links\n";

}
like image 404
Yanny Avatar asked Jun 13 '11 19:06

Yanny


2 Answers

Update 12 Dec 2016:

getResources().getDrawable() was deprecated in api 22 so you should now be using ContextCompat.getDrawable e.g.

Drawable d = ContextCompat.getDrawable(context, R.drawable.smiley);

In your activity you can call this to get your Drawable resource as a Drawable object

Drawable d = getResources().getDrawable(R.drawable.smiley);

If you want to show your drawable in an ImageView you can do this:

ImageView i = new ImageView(this);
i.setImageResource(R.drawable.smiley);

or in your xml file

<ImageView   
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/smiley"/>
like image 131
Damian Avatar answered Sep 27 '22 18:09

Damian


You can get the image with the help of pImg.Based on the adapter position get the string that is the image name.The image should be present in the drawable folder.

ImageView prodImg=(ImageView)view.findViewById(R.id.img_proj_name);
    String pImg=prod.get(position);
    int resID = view.getResources().getIdentifier("@drawable/"+pImg , "drawable", parentView.getContext().getPackageName());
    prodImg.setImageResource(resID);
like image 36
CoderDecoder Avatar answered Sep 27 '22 17:09

CoderDecoder