How to set a PNG file to an ImageView?
Asked Answered
B

4

13

I have a file called nochart.png in /drawable.

How can I set this to an ImageView?

chartImageView.setImageDrawable(R.drawable.nochart);

Does not compile.

Bassinet answered 29/9, 2010 at 2:30 Comment(0)
A
24

Use:

chartImageView.setImageResource(R.drawable.nochart);
Argilliferous answered 29/9, 2010 at 2:32 Comment(2)
Tried this, but still crashed when using a .png file. If I try the same thing with a .jpeg file, it works. The same is true (i.e crash) if I try android:background="@drawable/image_name" with a .png file, but works with .jpegs. Any ideas?Devoir
Fixed. The problem was the image size itself; it was too large. Scaling the image to be smaller did the trick and works from the XML file directly, i.e. android:background="@drawable/image_name"Devoir
S
3

you can also set the image using an xml file with the following attriblute

android:src="@drawable/nochart"
Schluter answered 29/9, 2010 at 4:52 Comment(3)
That doesn't work. It seems that ImageView can't load a png by default.Exemplum
Doing this works for me if I'm using a .jpeg file. It crashes with a .png file.Devoir
Scratch my last comment. My problem was the image size itself; it was too large. Scaling the image to be smaller did the trick and works from the XML file directly, i.e. android:background="@drawable/image_name" as a .png file.Devoir
I
1

I encountered the same problem. The solution for me was to move the ".png" from "drawable-21" to "drawable".

Insurgency answered 21/5, 2017 at 13:2 Comment(0)
J
1

just put your .png file in mipmap folder

Juba answered 18/1, 2021 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.