0
votes

I would like to display an image in the "Xamarin forms" but I got below error:

Image Loading: Error getting stream for http://www.example.com/example.jpg *(example only)*: System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error: NameResolutionFailure

I had tested that the URL of the image,and it was able to display on the browser.

Below is My code in code-behind:

var imageSource = new UriImageSource { Uri = new Uri("http://www.Example.com/example.jpg") } ;
            image.Source = imageSource;

And below is the code that I use in Xaml:

<Image x:Name="image" />

After I google for the solution,I found that ModernHttpClient might be a solution but I have no idea how to implement it.

Or isn't possible I could solve the error without using ModernHttpClient?

Please advice!

2
Error: NameResolutionFailure Whatever url you are actually using, the domain name was not resolved.... - SushiHangover

2 Answers

0
votes

Try this

string imageURI = "https://s9.postimg.org/aq1jt3fu7/handshake.87122244_std.jpg";
System.Uri uri;

async void LoadImage()
{
    System.Uri.TryCreate(imageURI, UriKind.Absolute, out uri);
    Task<ImageSource> result = Task<ImageSource>.Factory.StartNew(() => ImageSource.FromUri(uri));
    img.Source = await result;
}
0
votes

I have solved the problem.

It was because by default,the emulator was not configured to connect to internet,hence the image couldn't load from internet.

This is the solution to configure the emulator network card.