Cómo mostrar una imagen usando ImageView en 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.

preguntado el 08 de noviembre de 11 a las 13:11

5 Respuestas

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

Encontrarás el drawable folder(s) in the project res folder. You can put your image files there.

Respondido 18 Jul 14, 01:07

Thanks Michell, I created an Images folder in my project, inside of it there is a png image Black_x.png, So what should be the path here to decode image, it should be Images\Black_x.png ? - BreakHead

You should put your images in the drawable folder(s) in /res/. It'd be a good idea to use different sized images for each of the folders (i.e. hdpi and mdpi). Then use setImageResource(R.drawable.black_x) - Michell Bak

Once again Thanks, but when I am using R.id.imageViewId here I am not getting any id option means when I type R.id it says id is invalid - BreakHead

I really don't know how to explain it otherwise, as it's quite simple. Put an image file in your /res/drawable/ folder (if there's no drawable folder, use drawable-mhdpi or similar). Android will automatically create a reference to that image with its filename as the ID - i.e. R.id.black_x if your image file is called "black_x.jpg". - Michell Bak

@MichellBak and when does it create the ID file? - imsrgadich

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

Respondido 31 ago 16, 11:08

In res folder select the XML file in which you want to view your images,

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

Respondido 05 Jul 16, 20:07

debe ser @drawable/image dónde imagen could have any extension like: image.png, image.xml, image.gif. Android will automatically create a reference in R class with its name, so you cannot have in any drawable folder image.png and image.gif.

respondido 08 nov., 11:17

Drag image from your hard drive to Drawable folder in your project and in code use it like this:

ImageView image;

image = (ImageView) findViewById(R.id.yourimageviewid);
image.setImageResource(R.drawable.imagename);

contestado el 11 de mayo de 17 a las 12:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.