Forum Xamarin.Android

How can I set an image from URL to ImageView

Hi I'm newby in this. I want to set an image from a hosted site in a server of my network (the app will only work in our LAN). The URL is for instance: http://192.168.1.37:7000/ImagenesAPP/logo.png

I tried both of these:

row.FindViewById (Resource.Id.imgVideo).SetImageURI (Android.Net.Uri.Parse(rutaImg));

and

row.FindViewById (Resource.Id.imgVideo).SetImageBitmap(GetImageBitmapFromUrl(rutaImg));

private Bitmap GetImageBitmapFromUrl(string url)

    {
        Bitmap imageBitmap = null;

        using (var webClient = new WebClient())
        {
            var imageBytes = webClient.DownloadData(url);
            if (imageBytes != null && imageBytes.Length > 0)
            {
                imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
            }
        }

        return imageBitmap;

    }

The first one doesn't work and the second is for Url's in Internet (I think, because the method returns null).

Thanks in advance!

Tagged:

Answers

  • AlbertoZambranoAlbertoZambrano USMember
    edited June 2015

    Sorry guys, I solved the problem.

    Instead of using a string in: var imageBytes = webClient.DownloadData(url);
    I just needed to convert the url (string) to an URI, so the solution is: var imageBytes = webClient.DownloadData(new Uri(url));

  • guys i hev problem of imageurl
    hw to solution

Sign In or Register to comment.