【问题标题】:How to set image in imageview in android?如何在android的imageview中设置图像?
【发布时间】:2019-05-09 16:47:53
【问题描述】:

我想在 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 名称的图像。我想我把我的形象放在不合适的地方。我应该把我的图片放在哪里?

【问题讨论】:

标签: android image imageview


【解决方案1】:

您可以直接在您的 setimage 中将图像名称指定为 iv.setImageResource(R.drawable.apple); 应该是它。

【讨论】:

  • 你的意思是我应该写:ImageView iv= (ImageView)findViewById(R.id.img_selected_image); iv.setImageResource(R.drawable.apple);不需要其他代码?
  • 是的,不需要其他代码,但是请在res>drawable 中添加您的图像并在项目上执行Clean,以便它在您的R.java 文件中生成(自动)供参考。
  • 在您创建项目时会自动创建一个文件夹res,就在它下面您可能有一个文件夹drawable,或者您可以自己创建。
【解决方案2】:

使用下面的代码,

    iv.setImageResource(getResources().getIdentifier("apple", "drawable", getPackageName()));

【讨论】:

    【解决方案3】:

    您也可以在 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);
      

      【讨论】:

        【解决方案5】:

        您放入 res/drawable 的图像由 Android 处理。您无需按照您的方式获取图像。 在你的情况下,你可以简单地打电话给iv.setImageRessource(R.drawable.apple)

        只获取图片(不直接添加到ImageView),可以调用Context.getRessources().getDrawable(R.drawable.apple)获取图片

        【讨论】:

          【解决方案6】:

          如果你想将位图对象设置为图像视图,这里是简单的两行`

          Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sample_drawable_image);
          image.setImageBitmap(bitmap);
          

          【讨论】:

            【解决方案7】:
                    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
            

            【讨论】:

              【解决方案8】:

              如果您从 Java 类创建 ImageView

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

              【讨论】:

                【解决方案9】:

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

                OR 片段

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

                【讨论】:

                  猜你喜欢
                  • 2012-09-19
                  • 1970-01-01
                  • 2013-07-07
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多