Home > Software design >  Xamarin forms Imagesource not working on Android
Xamarin forms Imagesource not working on Android

Time:11-30

So I am working on a project and want to get an image from my server to the application. I use an imagesource to get the image I need and it works fine on UWP but when I test it on Android it does not work. I tried everything I could find on the internet and it is still not working.

If you could help that would be great, Thank you.

ImageSource fotoopad = ImageSource.FromFile("uploadplaceholder.png");
if (x.FotoPath != "") fotoopad = ImageSource.FromUri(new Uri(XamerinAPP.LoginApp.apiUrl   @"/api/media/image/"   x.Id));                    

and the xamarin.

<Image x:Name="StackMediaIMAGE" IsVisible="{Binding isFoto}" Source="{Binding fotoPad}"/>

CodePudding user response:

What's the code of the bind property fotoPad for the Image?

In general, if we want to bind a property to an image, we can do as follows:

1.add a property fotoPad in the viewModel of current page.

for example:

public class Item
{       
    public string fotoPad { get; set; } 

    public bool isFoto{ get; set; }
}

2.and a correct format of fotoPad is as follows:

new Item
        {  
            isFoto = true,
            fotoPad = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Alouatta_guariba.jpg/200px-Alouatta_guariba.jpg"
        }

CodePudding user response:

If you are working on Xamarin for Android try getting the Uri the following way:

Android.Net.Uri photoUri = Android.Net.Uri.Parse($"{ContentResolver.SchemeAndroidResource}://{PackageName}/{<YourPackageName>.Droid.Resource.Raw.<YourPngFileName>}");

Replace < YourPackageName > and < YourPngFileName > for the concrete data in your project.

Save the image.png (for example) under Resources/raw folder in Android.

  • Related