问题
I want to show an image in imageview so I made a folder - drawable in res and put my image there. Something like apple.png. Then I use this code:
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);
But when i run it, it said there isn't any image by apple.png name in data/data/package-name/files. I think i put my image in inappropriate place. Where should i put my image ?
回答1:
you can directly give the Image name in your setimage as iv.setImageResource(R.drawable.apple); that should be it.
回答2:
use the following code,
iv.setImageResource(getResources().getIdentifier("apple", "drawable", getPackageName()));
回答3:
Instead of setting drawable resource through code in your activity class you can also set in XML layout:
Code is as follows:
<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:
The images your put into res/drawable are handled by Android. There is no need for you to get the image the way you did.
in your case you could simply call iv.setImageRessource(R.drawable.apple)
to just get the image (and not adding it to the ImageView directly), you can call Context.getRessources().getDrawable(R.drawable.apple) to get the image
回答6:
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
回答7:
If you created ImageView from Java Class
ImageView img = new ImageView(this);
//Here we are setting the image in image view
img.setImageResource(R.drawable.my_image);
回答8:
if u want to set a bitmap object to image view here is simple two line`
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sample_drawable_image);
image.setImageBitmap(bitmap);
回答9:
1> You can add image from layout itself:
<ImageView
android:id="@+id/iv_your_image"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:background="@mipmap/your_image"
android:padding="2dp" />
OR
2> Programmatically in java class:
ImageView ivYouImage= (ImageView)findViewById(R.id.iv_your_image);
ivYouImage.setImageResource(R.mipmap.ic_changeImage);
OR for fragments:
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);
回答10:
set image in imageview
imageView.setImageResource(R.drawable.myImage);
来源:https://stackoverflow.com/questions/13513953/how-to-set-image-in-imageview-in-android