How to set image in imageview in android?-ThrowExceptions

Exception or error:

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

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 ?

How to solve:

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


use the following code,

    iv.setImageResource(getResources().getIdentifier("apple", "drawable", getPackageName()));


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

Code is as follows:

        android:src="@drawable/apple" />


// take variable of imageview

private ImageView mImageView;

//bind imageview with your xml's id

mImageView = (ImageView)findViewById(;

//set resource for imageview



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(

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


        ImageView iv= (ImageView)findViewById(;
        public static int getDrawable(Context context, String name)//method to get id
        return context.getResources().getIdentifier(name,    //return id
            "your drawable", context.getPackageName());
        image.setImageResource(int Id);//set id using this method


If you created ImageView from Java Class

ImageView img = new ImageView(this);

//Here we are setting the image in image view


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


1> You can add image from layout itself:

                android:padding="2dp" />


2> Programmatically in java class:

ImageView ivYouImage= (ImageView)findViewById(;

OR for fragments:

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

ImageView ivYouImage= (ImageView) rowView.findViewById(;

Leave a Reply

Your email address will not be published. Required fields are marked *