24

I want to show an image in imageview so I made a folder - drawable in res and put my image there. Something like apple.png. Then I use this code:

ImageView iv= (ImageView)findViewById(R.id.img_selected_image);
String path = getApplication().getFilesDir().getAbsolutePath();
InputStream is = new FileInputStream(path + "/apple.png");
Drawable icon = new BitmapDrawable(is);
Log.i("Fnord", "width="+icon.getIntrinsicWidth()+
     " height="+icon.getIntrinsicHeight());
iv.setImageDrawable(icon);

But when i run it, it said there isn't any image by apple.png name in data/data/package-name/files. I think i put my image in inappropriate place. Where should i put my image ?

Sergius
  • 521
  • 8
  • 21
shadi
  • 431
  • 3
  • 5
  • 15
  • You also need to make sure that your image isn't going to be stretched https://themillibit.wordpress.com/2017/09/25/why-are-my-bitmaps-all-stretched/ – Ruchir Baronia Oct 01 '17 at 00:11

9 Answers9

79

you can directly give the Image name in your setimage as iv.setImageResource(R.drawable.apple); that should be it.

Anuj
  • 2,065
  • 22
  • 23
  • you mean that i should just write: ImageView iv= (ImageView)findViewById(R.id.img_selected_image); iv.setImageResource(R.drawable.apple); another code don t need? – shadi Nov 22 '12 at 13:51
  • Yes, other code isn't required, but please add your Image in the `res>drawable` and do a `Clean` on the project so that it gets Generated in your `R.java` file (automatically) for referencing. – Anuj Nov 22 '12 at 13:53
  • There is a folder as `res` created automatically when you create your project, just under that you might be having a folder as `drawable` or you can create it yourself. – Anuj Nov 22 '12 at 13:56
8

use the following code,

    iv.setImageResource(getResources().getIdentifier("apple", "drawable", getPackageName()));
Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
8

Instead of setting drawable resource through code in your activity class you can also set in XML layout:

Code is as follows:

<ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/apple" />
Rajendra
  • 484
  • 4
  • 19
4
// take variable of imageview

private ImageView mImageView;

//bind imageview with your xml's id

mImageView = (ImageView)findViewById(R.id.mImageView);

//set resource for imageview

mImageView.setImageResource(R.drawable.your_image_name);
Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
2

The images your put into res/drawable are handled by Android. There is no need for you to get the image the way you did. in your case you could simply call iv.setImageRessource(R.drawable.apple)

to just get the image (and not adding it to the ImageView directly), you can call Context.getRessources().getDrawable(R.drawable.apple) to get the image

Sprigg
  • 3,340
  • 1
  • 18
  • 26
2

if u want to set a bitmap object to image view here is simple two line`

Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sample_drawable_image);
image.setImageBitmap(bitmap);
Priyatam
  • 21
  • 4
1
        ImageView iv= (ImageView)findViewById(R.id.img_selected_image);
        public static int getDrawable(Context context, String name)//method to get id
        {
         Assert.assertNotNull(context);
         Assert.assertNotNull(name);
        return context.getResources().getIdentifier(name,    //return id
            "your drawable", context.getPackageName());
        }
        image.setImageResource(int Id);//set id using this method
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

If you created ImageView from Java Class

ImageView img = new ImageView(this);

//Here we are setting the image in image view
img.setImageResource(R.drawable.my_image);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Lakshay Sharma
  • 1,347
  • 12
  • 13
0

1> You can add image from layout itself:

<ImageView
                android:id="@+id/iv_your_image"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:background="@mipmap/your_image"
                android:padding="2dp" />

OR

2> Programmatically in java class:

ImageView ivYouImage= (ImageView)findViewById(R.id.iv_your_image);
        ivYouImage.setImageResource(R.mipmap.ic_changeImage);

OR for fragments:

View rowView= inflater.inflate(R.layout.your_layout, null, true);

ImageView ivYouImage= (ImageView) rowView.findViewById(R.id.iv_your_image);
        ivYouImage.setImageResource(R.mipmap.ic_changeImage);
Tarit Ray
  • 944
  • 12
  • 24