Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show image using ImageView in Android

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.

like image 977
BreakHead Avatar asked Nov 08 '11 13:11

BreakHead


People also ask

Can ImageView display video?

You can add ImageView and VideoView in RelativeLayout and set ImageView to invisible and VideoView to visible and vice-versa and you can play video on onClick.


2 Answers

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.

like image 142
Michell Bak Avatar answered Sep 25 '22 20:09

Michell Bak


You can set imageview in XML file like this :

<ImageView     android:id="@+id/image1"     android:layout_width="wrap_content"      android:layout_height="wrap_content"     android:src="@drawable/imagep1" /> 

and you can define image view in android java file like :

ImageView imageView = (ImageView) findViewById(R.id.imageViewId); 

and set Image with drawable like :

imageView.setImageResource(R.drawable.imageFileId); 

and set image with your memory folder like :

File file = new File(SupportedClass.getString("pbg")); if (file.exists()) {         BitmapFactory.Options options = new BitmapFactory.Options();         options.inPreferredConfig = Bitmap.Config.ARGB_8888;         Bitmap selectDrawable = BitmapFactory.decodeFile(file.getAbsolutePath(), options);         imageView.setImageBitmap(selectDrawable); } else {       Toast.makeText(getApplicationContext(), "File not Exist", Toast.LENGTH_SHORT).show(); } 
like image 20
Pankaj Talaviya Avatar answered Sep 24 '22 20:09

Pankaj Talaviya