shadi shadi - 3 months ago 14
Android Question

How to set image in imageview in android?

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 ?

Answer

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