Reputation: 1122
I cannot get my images to show up in Android at all. I have tried with ImageSource.FromFile and ImageSource.FromResource.
I have read "Working with Images" several times and just don't see what I am doing wrong.
From File Example
var myImage = new Image() {
Aspect = Aspect.AspectFill,
Source = ImageSource.FromFile("backdrop.jpg")
};
RelativeLayout layout = new RelativeLayout();
layout.Children.Add(myImage,
Constraint.Constant(0),
Constraint.Constant(0),
Constraint.RelativeToParent((parent) => { return parent.Width; }),
Constraint.RelativeToParent((parent) => { return parent.Height; }));
Content = layout;
The Build Action is set to: AndroidResource
I always get the NullReference exception with this path
FromResource example
var myImage = new Image()
{
Aspect = Aspect.AspectFill,
Source = ImageSource.FromResource("TTMD_Mobile.Droid.backdrop.jpg")
};
RelativeLayout layout = new RelativeLayout();
layout.Children.Add(myImage,
Constraint.Constant(0),
Constraint.Constant(0),
Constraint.RelativeToParent((parent) => { return parent.Width; }),
Constraint.RelativeToParent((parent) => { return parent.Height; }));
Content = layout;
The Build Action is set to: Embedded Resource
With the FromResource I don't get any exception, just the image doesn't show.
I am fairly new to Xamarin and Android development, my apologies if this has been addressed, I couldn't find a working solution.
EDIT: I do not believe this is a duplicate of that question, I have my images in their correct locations I believe.
Upvotes: 1
Views: 5233
Reputation: 136
Create images folder in Xamarin forms project and then add Image to that folder. In properties for the image, set "Build Action" to "Embedded resource"
For ex: My project name is XTest. Path to image is: XTest/images/pic.jpg
In you cs file you can now use it like this.
imgLocal.Source = ImageSource.FromResource("XTest.images.pic.jpg");
Upvotes: 3