Search code examples
c#xamarin.formsxamarin.androidxamarin.iosvisual-studio-2019

Xamarin external image not displaying problem


I am trying to add images to Xamarin project on Visual Studio 2019. Images are displaying from local storage but external links. I need to be able display an image from an image url.

What i have trieds:

build app on android apis 27,28
check options for ssl implementation is Native TLS 1.2+
change the site ssl status http,https.
change the image and image urls from different sites.
another image extensions jpg,jpeg,png,...
change image sizes between 50 kb and 3mb.

When i uninstalled and rebuild the app, In first time run app throw this error:

Image Loading: Error getting stream for https://p-valid.com/images/logo.png:
System.ObjectDisposedException:

In my opinion, this error is a bug in Xamarin.

add this code to AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

Only way the image shows when I put the image to drawable folder then get it by its name:

var image = new Image { Source =  "test.jpg"};

My main code is:

var layout = new StackLayout { Padding = new Thickness(5, 10) };

var label = new Label { Text = "Hello world" };
var image = new Image { 
            Source = ImageSource.FromUri(new Uri("http://mydatabase/test.jpg"))
};

layout.Children.Add(label);
layout.Children.Add(image);

this.Content = layout;

What i expect from this code is:
Write Hello world and then show image

But its only showing Hello world and there is no image.
Also on debug output console writing this error:

ImageLoaderSourceHandler: Could not retrieve image or image data was invalid: Uri http://mydatabase/test.jpg

In my opinion, this code is about wrong cached image data.


Solution

  • In the start , the new created project about the version of xamarin forms is 4.2. However , it occurs the error :

    [0:] ImageLoaderSourceHandler: Could not retrieve image or image data was invalid: Uri: https://aka.ms/campus.jpg

    However after checking the latest version of Xamarin Forms in project . You can update it to the latest version : 4.3.0.908675 to check whether it works .

    Then the following code works in my local project:

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
    
            var layout = new StackLayout { Padding = new Thickness(5, 10) };
    
            var label = new Label { Text = "Hello world" };
            var image = new Image
            {
                Source = ImageSource.FromUri(new Uri("https://s2.ax1x.com/2019/10/28/K6KUo9.png")),
                BackgroundColor = Color.Accent
            };
    
            layout.Children.Add(label);
            layout.Children.Add(image);
    
            Content = layout;
        }
    }
    

    The permission needs to be added in manifest:

    <uses-permission android:name="android.permission.INTERNET" />
    

    Effect :

    enter image description here

    Note : Better not loading large size of image. It also will results of error loading .