How to show image using ImageView in Android

BreakHead picture BreakHead · Nov 8, 2011 · Viewed 143.9k times · Source

I am looking for the way to assign image src to image view control. I read few example and they says something src="@drawable\image" but didn't understand this, also I want to assign image src at runtime by java code also want to apply default image in XML.

Answer

Michell Bak picture Michell Bak · Nov 8, 2011

If you want to display an image file on the phone, you can do this:

private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));

If you want to display an image from your drawable resources, do this:

private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageResource(R.drawable.imageFileId);

You'll find the drawable folder(s) in the project res folder. You can put your image files there.