13

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

}
Yanny
  • 163
  • 1
  • 2
  • 8

6 Answers6

37

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"/>
Damian
  • 8,062
  • 4
  • 42
  • 43
  • The problem is I don't have a default place to declare the ImageView, because the ImageView needs to be created dynamically in the code. Based on the if decisions there will be different directions including images to be displayed. – Yanny Jun 15 '11 at 06:11
  • Have you looked at this? http://stackoverflow.com/questions/2865452/is-it-possible-to-display-inline-images-from-html-in-an-android-textview – Damian Jun 15 '11 at 09:49
  • Yes certainly, but still not working. I found a solution anyway, that is simply by using SpannableStringBuilder. This is a very simple solution to use to insert images and into a textview! – Yanny Jun 17 '11 at 20:37
  • Hi yanny, i can able able to Draw the image one by one . But i want to draw more than one images simultaneously and set it in to text view. i have try it with getter method. but not successes. if you have resolve this type of problem then plz help me. – amity Sep 08 '11 at 13:16
  • 1
    getResources().getDrawable(); is deprecated in api 22. You should use the ContextCompat solution. – JFouad Dec 11 '16 at 16:51
6

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);
CoderDecoder
  • 445
  • 4
  • 18
1

A super easy way to show it is to write this in your ImageView widget in your xml:

android:background="@drawable/your_file_name"

... and make sure you have put that image file (whether png or jpg, etc.) into your drawable folder. When you write your_file_name, you only need the title, not the file extension.

I prefer this way over programmatically writing in your .java file (as shown with other answers above), because your xml will show you the image in preview, programmatically will not, it will just show the ImageView placeholder.

Azurespot
  • 3,066
  • 3
  • 45
  • 73
0

Easiest way - Can be consider the below code

We can take advantage of Imageview setImageResource , refer below code for the same. put image in the drawable like the below order

image_1.png, image_2.png, etc.

int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", getPackageName());
             imageview.setImageResource(resId);
Lakshmanan
  • 1,671
  • 2
  • 26
  • 42
0
  Drawable drawable = getResources().getDrawable(R.mydrawableID);

Now you can use it as Image, Like:-

ImageView myImage = findViewById(R.id.yourImageView);

Now fire this -

myImage.setImageResource(drawable);
Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19
0

You should use ContextCompat for backward compatibility features on Android.

//Using ButterKnife
@BindView(R.id.DogImg)
    ImageView myImage;
//Or 
  ImageView myImage = findViewById(R.id.MyImage);
...
Drawable MyImageDrw = ContextCompat.getDrawable(context,R.drawable.myImageOnResources);
myImage.setImageDrawable(MyImageDrw);