9

I have a Xamarin Forms Android app and when I'm using the following code

 var image = new Image { Source = "lock.png" }; 

to show an image, but the image isn't showing though.

And then I try to change the source to Icon.png (the default Xamarin icon) and it working fine.

Does anybody know how to make this image show in my app?

(ps: image path : ".\MyApp.Droid\Resources\drawable\lock.png".)

1

4 Answers 4

13

Make sure that image has Build Action set to "Android Resource". Otherwise, it is not copied to final package properly and there is no image for app to show. This is described in guide here: https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Local_Images

Sign up to request clarification or add additional context in comments.

1 Comment

Hello. In my situation, it seems that the svgs / images are not displayed on Android 12 / 13, but works on all the other versions. Do you know any reason why that might happen? Thanks in advance.
7

I also faced to this issue. I tried above mentioned two solutions too. But didn't. People can try below suggestions one by one,

  • In Solution Explorer -> right click on image -> select "Include to project"
  • Go to image properties -> Build Action -> set to "Android Resource"
  • Check whether your image file name contains dashes or hyphens
  • Clean the project
  • Build the project and Run the project

I didn't build the project, after adding the image. It was my problem. May someone get benefited from my answer.

1 Comment

Clean and rebuild did it for me
2

For others who may come here, also check to make sure your image doesn't have dashes/hyphens in the file name as per https://stackoverflow.com/a/35760195/908677 - that was why my image wasn't showing up.

Comments

2

Another problem could be that the image is too large to be rendered. If everything else fails, try creating a smaller version of the image.

I hope this helps somebody.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.