How to show image using ImageView in Android

前端 未结 5 1521
故里飘歌
故里飘歌 2020-12-08 04:27

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 t

相关标签:
5条回答
  • 2020-12-08 05:05

    shoud be @drawable/image where image 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.

    0 讨论(0)
  • 2020-12-08 05:12

    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.

    0 讨论(0)
  • 2020-12-08 05:14

    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();
    }
    
    0 讨论(0)
  • 2020-12-08 05:16

    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" />
    
    0 讨论(0)
  • 2020-12-08 05:20

    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);
    
    0 讨论(0)
提交回复
热议问题