如何在Android中设置ImageView中的图像?

25

我想在ImageView中显示一张图片,所以我在res中创建了一个名为drawable的文件夹,并把我的图片放在里面。类似于apple.png。然后我使用以下代码:

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

但是当我运行它时,它说在 data/data/package-name/files 中找不到名为 apple.png 的任何图像。我想我把我的图像放在了不合适的地方。我应该把我的图像放在哪里?


你还需要确保你的图像不会被拉伸 https://themillibit.wordpress.com/2017/09/25/why-are-my-bitmaps-all-stretched/ - Ruchir Baronia
9个回答

79

您可以直接在setimage中使用图像名称,如:iv.setImageResource(R.drawable.apple); 这样就可以了。


你的意思是我只需要写这样的代码:ImageView iv= (ImageView)findViewById(R.id.img_selected_image); iv.setImageResource(R.drawable.apple); 没有其他代码了吗? - shadi
是的,不需要其他代码,但请将您的图像添加到“res>drawable”中,并在项目上执行“Clean”操作,以便它会自动在您的“R.java”文件中生成(供引用)。 - Anuj
当您创建项目时,会自动创建一个名为“res”的文件夹,就在该文件夹下面,您可能会有一个名为“drawable”的文件夹,或者您可以自己创建它。 - Anuj

8
使用以下代码:
    iv.setImageResource(getResources().getIdentifier("apple", "drawable", getPackageName()));

8

您不必在活动类中使用代码设置drawable资源,也可以在XML布局中设置:

以下是代码:

<ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/apple" />

4
// take variable of imageview

private ImageView mImageView;

//bind imageview with your xml's id

mImageView = (ImageView)findViewById(R.id.mImageView);

//set resource for imageview

mImageView.setImageResource(R.drawable.your_image_name);

2

您放置在res/drawable文件夹中的图像将由Android处理。您不需要以您所做的方式获取图像。

在您的情况下,您可以简单地调用iv.setImageRessource(R.drawable.apple)来获取图像(而不是直接将其添加到ImageView中)。

要仅获取图像(而不将其直接添加到ImageView中),您可以调用Context.getRessources().getDrawable(R.drawable.apple)来获取该图像。


2
如果您想将位图对象设置到ImageView上,这里有两行简单的代码。
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sample_drawable_image);
image.setImageBitmap(bitmap);

1
        ImageView iv= (ImageView)findViewById(R.id.img_selected_image);
        public static int getDrawable(Context context, String name)//method to get id
        {
         Assert.assertNotNull(context);
         Assert.assertNotNull(name);
        return context.getResources().getIdentifier(name,    //return id
            "your drawable", context.getPackageName());
        }
        image.setImageResource(int Id);//set id using this method

1
如果你通过Java类创建了ImageView
ImageView img = new ImageView(this);

//Here we are setting the image in image view
img.setImageResource(R.drawable.my_image);

0

1> 你可以直接从布局中添加图片:

<ImageView
                android:id="@+id/iv_your_image"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:background="@mipmap/your_image"
                android:padding="2dp" />

或者

2> 在Java类中以编程方式:

ImageView ivYouImage= (ImageView)findViewById(R.id.iv_your_image);
        ivYouImage.setImageResource(R.mipmap.ic_changeImage);

对于片段

View rowView= inflater.inflate(R.layout.your_layout, null, true);

ImageView ivYouImage= (ImageView) rowView.findViewById(R.id.iv_your_image);
        ivYouImage.setImageResource(R.mipmap.ic_changeImage);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接