How to show image using ImageView in Android - 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.

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.

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

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" />

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.

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

Related

get ImageView src programmatically

How to get ImageView src programmatically and set it in another ImageView
Drawable drawable = imageViewA....?
imageViewB.setImageDrawable(drawable);
You can do something like this:
Drawable drawable = imageViewA.getDrawable();
if(drawable != null){
imageViewB.setImageDrawable(drawable);
}
You can use setImageResource(int)
imageView.setImageResource(R.drawable.bg_image);
Instead of get drawable you do...
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
and set to new image
newImageView.setImageBitmap(bitmap);

Android: Image from assets folder is smaller than it should be

I have problem when I try to set an image to a ImageView browsed from assets folder. When I place the same image in the drawable folder and use imageView.setImageResource(R.id.image); it is larger and fits the screen better than the image browsed by the previous method. Is there a way to resize the image so that it fits the screen exactly the same way as usual.
Here is the layout file and the code in java.
AssetManager manager = getActivity().getAssets();
InputStream input = null;
try {
input = manager.open("images/" + mQuestion.getQImage() + ".png");
} catch (IOException e) {
e.printStackTrace();
}
Bitmap image = BitmapFactory.decodeStream(input);
ImageView qImageView = (ImageView) questionView.findViewById(R.id.ImageView_qImage);
qImageView.setImageBitmap(image);
<ImageView
android:id="#+id/ImageView_qImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#dimen/questionTopBottomMargin"
android:background="#color/radioButtonStrokeColor"
android:contentDescription="#string/hello_world"
android:padding="1px"
android:scaleType="fitXY" />
When you place an image in drawable folder then the images are classified on density of screen while if you put images in assest folder then android need to calculate density.
If you want to use images from assest folder then this code will help you.
Options opts = new BitmapFactory.Options();
opts.inDensity = DisplayMetrics.DENSITY_HIGH;
drawable = Drawable.createFromResourceStream(context.getResources(), null, is, srcName, opts);
see here
Image loaded from assets folder comes in different size than res/drawable
Android: Accessing images from assets/drawable folders
If you place images in the drawables folder they will be scaled by some functions it they load them from resources. This will not happen with images in assets folder which you load yourself.
Have a look at android docs
You should always use images from the drawable folder
There are different drawable folders in android
(drawable-hdpi,drawable-mdpi,drawable-ldpi)
Android will decide which folder image should be drawn depending on
the phone requirements and resolution
Use 9-patch image for auto-scaling
When you load from assets, the "source" density is unknown, so this
autoscaling is not performed. Hence, there is difference.
You just define the imageView width and height in the xml layout then you should try this code it works perfectly for me
public Bitmap getImageBitmap(String imgName){
AssetManager mgr = getAssets();
InputStream inputStream = null;
try {
inputStream = mgr.open(imgName + ".png");
return BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = getImageBitmap(imageName);
imageView.setImageBitmap(bitmap);

Linearlayout background image in assets folder

I want to use a Image from the assets folder as my background image in android
but the only picture I can use is out oif the drawable folder
random = String.valueOf(util_random.random(1, 4));
String drawable = "/assets/images/img"+random ;
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundDrawable(drawable);
this.setContentView(ll);
You cannot pass a String in View.setBackgroundDrawable(Drawable).
Use BitmapFactory to create a Bitmap, then create a BitmapDrawable and use it as your background image.

can we pass a directory path to Drawable.createfrompath instead of a file path?

can we pass a directory path to Drawable.createfrompath instead of a file path?
Ex:
Drawable d = Drawable.createFromPath("aq/img/sample.png");
converts the path to a drawable "d". I want to pass a folder path to the same function, say "/aq/img/" and I want convert all images present in that folder into a drawable. Can this be done? and how?
EDIT:
Did this to display a single image in my imageView.
ImageView imgView01 = (ImageView) findViewById(R.id.imageView1);
File dir = new File("/sdcard/WallpapersHD/");
File file[]=dir.listFiles();
for (int i=0;i<file.length;i++) {
Drawable d = (Drawable) Drawable.createFromPath(file[i].toString());
imgView01.setImageDrawable(d);
Now how do I set a delay of say 5 seconds and then set a different image in the imageview?
After do this to start to count 5 seconds
long time = SystemClock.elapsedRealTime();
put this into a loop:
if(SystemClock.elapsedRealtime() > time + 5000){
ImageView img01 = (ImageView) findViewById(R.id.yourNewImg);
img01.setImageDrawable(d);
}
"d" is your Drawable.

Android - How to show images from resources drawable?

I'm not able to show an image which is saved in res/drawable folder.
I use ImageGetter to do this. The code is below:
ImageGetter imageGetter3 = new ImageGetter() {
public Drawable getDrawable(String source) {
int id=0;
if (source.equals("smiley")) {
id = R.drawable.smiley;
}
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n"
+ "Sta met je rug voor de deur\n"
+ Html.fromHtml("<img src=\"smiley\">", imageGetter3, null) + " Draai naar links\n";
What I see on the screen when running is a little square with "obj" text on it.
So what is wrong? The image cannot be read or something else?
How to show images?
Honestly I have Googled a lot and tried other methods of ImageGetter as well, but none of them seems to work, I tried these too, they don't work:
ImageGetter imageGetter2 = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = null;
d = Drawable.createFromPath(source);
d.setBounds(0,0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawFromPath;
int path = Route.this.getResources().getIdentifier(source, "drawable","com.prj56.tracingapp");
drawFromPath = (Drawable) Route.this.getResources().getDrawable(path);
drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(), drawFromPath.getIntrinsicHeight());
return drawFromPath;
}
};
=========================================================
if (....) {
ImageView iv1 = new ImageView(this);
iv1.setImageResource(R.drawable.smiley);
directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n"
+ "Sta met je rug voor de deur\n";
HERE COMES THE IMAGE! BUT HOW TO DO THIS? It's within directions textview...
directions += " Draai naar links\n";
}
Update 12 Dec 2016:
getResources().getDrawable() was deprecated in api 22 so you should now be using ContextCompat.getDrawable e.g.
Drawable d = ContextCompat.getDrawable(context, R.drawable.smiley);
In your activity you can call this to get your Drawable resource as a Drawable object
Drawable d = getResources().getDrawable(R.drawable.smiley);
If you want to show your drawable in an ImageView you can do this:
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.smiley);
or in your xml file
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/smiley"/>
You can get the image with the help of pImg.Based on the adapter position get the string that is the image name.The image should be present in the drawable folder.
ImageView prodImg=(ImageView)view.findViewById(R.id.img_proj_name);
String pImg=prod.get(position);
int resID = view.getResources().getIdentifier("#drawable/"+pImg , "drawable", parentView.getContext().getPackageName());
prodImg.setImageResource(resID);
A super easy way to show it is to write this in your ImageView widget in your xml:
android:background="#drawable/your_file_name"
... and make sure you have put that image file (whether png or jpg, etc.) into your drawable folder. When you write your_file_name, you only need the title, not the file extension.
I prefer this way over programmatically writing in your .java file (as shown with other answers above), because your xml will show you the image in preview, programmatically will not, it will just show the ImageView placeholder.
Easiest way - Can be consider the below code
We can take advantage of Imageview setImageResource , refer below code for the same.
put image in the drawable like the below order
image_1.png, image_2.png, etc.
int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", getPackageName());
imageview.setImageResource(resId);
Drawable drawable = getResources().getDrawable(R.mydrawableID);
Now you can use it as Image, Like:-
ImageView myImage = findViewById(R.id.yourImageView);
Now fire this -
myImage.setImageResource(drawable);
You should use ContextCompat for backward compatibility features on Android.
//Using ButterKnife
#BindView(R.id.DogImg)
ImageView myImage;
//Or
ImageView myImage = findViewById(R.id.MyImage);
...
Drawable MyImageDrw = ContextCompat.getDrawable(context,R.drawable.myImageOnResources);
myImage.setImageDrawable(MyImageDrw);

Categories

Resources