Show image from URL with Xamarin.Forms

28,843

Solution 1

Well you can do this thing easier with your Xaml just make your xaml like this

<Image x:Name="backgroundImage" Source="https://s9.postimg.org/aq1jt3fu7/handshake_87122244_std.jpg" AbsoluteLayout.LayoutBounds="0,0,1,1"   AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill"/>

And remove the code related in the code behind. The cashing is enabled by default for 24 hours

Solution 2

It's not working because you use a https url. For fix it, you should be configure your Android Project like this : In Project Options > Android Options, click Advanced Options HttpClient implementation : choose Android SSL/TLS implementation : choose Native TLS 1.2+

https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=windows

And update all Xamarin.Android packages

Share:
28,843

Related videos on Youtube

Kishore Suthar
Author by

Kishore Suthar

Objective-C Swift Autolayout Data Structure and Algorithm MPV, MVVM Unit Test UITest

Updated on July 04, 2020

Comments

  • Kishore Suthar
    Kishore Suthar almost 4 years

    I am using this code to show image from an URL

    .xaml

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="LandAHand.VolunteerView">
        <ContentPage.Content>
             <AbsoluteLayout BackgroundColor="Maroon">
                 <Image x:Name="backgroundImage" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill" />
             </AbsoluteLayout>
        </ContentPage.Content>
    </ContentPage>
    

    .cs

    using System;
    using System.Collections.Generic;
    
    using Xamarin.Forms;
    
    namespace LandAHand
    {
        public partial class VolunteerView : ContentPage
        {
            public VolunteerView()
            {
                InitializeComponent();
                backgroundImage.Source = new UriImageSource
                {
                    Uri = new Uri("https://s9.postimg.org/aq1jt3fu7/handshake_87122244_std.jpg"),
                    CachingEnabled = true,
                    CacheValidity = new TimeSpan(5, 0, 0, 0)
                };
            }
        }
    }
    

    This code is successfully working with iOS but it is not working with Android.

    • Rui Marinho
      Rui Marinho over 7 years
      i just tested this and it works.
  • Kishore Suthar
    Kishore Suthar over 7 years
    Working fine in IOS device but not working in android device
  • Ahmad ElMadi
    Ahmad ElMadi over 7 years
    how about if you put it inside a stacklayout instead of absolute? and try it on different devices
  • Nabil Akhlaque
    Nabil Akhlaque over 2 years
    did not work for me