image from url needing auth

Hi!
I have problems to get images from URL (https://) on Android. For getting data (.csv-file) from the same URL I successfully use Webcllient like this.

WebClient webClient = new WebClient(); webClient.Credentials = new NetworkCredential(username, password, domain); ServicePointManager.ServerCertificateValidationCallback = ServerCertificateValidationCallback; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

In my XAML-code I use this.

<Image x:Name="Img_Small"> <Image.Source> <UriImageSource Uri="{Binding ImgSmall}" /> </Image.Source> </Image>

So my guess is, that the app got no Access to URL because of missing auth. Is it possible to auth the app permanent to the URL, when starting the app and to dispose the app, when closing it?

Sorry for my bad English..

Best Answers

Answers

  • MamagotchiMamagotchi Member ✭✭

    @TaylorD said:
    If you have data (.csv file) coming from the URL, download the image via that method as bytes and bind the Source property on Image to an ImageSource in your ViewModel.

    Thanks, this helps a lot! So here is my c#-code:
    ` WebClient webClient = new WebClient();
    webClient.Credentials = new NetworkCredential(username, passwort, domain);
    ServicePointManager.ServerCertificateValidationCallback = ServerCertificateValidationCallback;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            ImageSource retSource = null;
    
            byte[] imageBytes = webClient.DownloadData(url);
    
            retSource = ImageSource.FromStream(() => new MemoryStream(imageBytes));
            Img_Test.Source = retSource;`
    

    The image is now displayed in the app. But I just not figure out, how to bind it using ViewModel. I create a class "Image_Class"
    public class Image_Class { public byte[] ImageBytes { get; set; } }

    so in C# I could use something like this, couldn't I?
    Image_Class imageBytes = new Image_Class(); imageBytes.ImageBytes = webClient.DownloadData(url);

    So I get Image as byte[] in my Image_Class. But how to bind it in XAML? Just don't get it... ;-(

  • MamagotchiMamagotchi Member ✭✭

    I am such a stupid guy... I forgot to set BindingContext to my ViewModel... :#

    Thanks for your patience and your helpful hints!

  • TaylorDTaylorD USMember ✭✭✭

    That issue has happened to all of us. Glad I could help get it working for ya! Good luck with your project.

  • MamagotchiMamagotchi Member ✭✭

    Yes it's working in my project. Unfortunately it is quite slow. In my real project i got where you can type in what you are searching. This Entry is trigger an TextChanged-event. This event compares the string with data of a csv-file. if some conditions a true the data (line) add to a list, which ist binded to a .

  • TaylorDTaylorD USMember ✭✭✭

    In that series of events, when is the image being downloaded? If it is when the data is added to the list, consider asynchronously downloading the image in the background to keep the UI responsive while the image is downloading.

  • MamagotchiMamagotchi Member ✭✭

    Yes it's loading image in case of a match.

    loading image as byte[] --> Converting in to an ImageSource --> Add it to my ModelView

    For asynchronously downloading, is this wright?
    byte[] imageBytes = webClient.DownloadData(url);

  • MamagotchiMamagotchi Member ✭✭
    ...
    byte[] imageBytes = webClient.DownloadDataAsync(url);
  • MamagotchiMamagotchi Member ✭✭
    Okay, I use an async Task and it works. But my ListView updates its content in 2 steps.
    Step1: It lists all text of my ViewModel at once
    Step2: It lists all images and all text of my ViewModel at once

    Feels very crappy. It would be better, if it will show image and text, having a match in my c#-Code. I guess I have to edit my ViewModel, haven't I?
Sign In or Register to comment.