Ngo Kim Huynh
Ngo Kim Huynh

Reputation: 79

How to download image from url in xamarin android?

I'm trying to download an image from URL. This is my code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edt_main_url"
        android:hint="URL"
        android:text="http://xamarin.com/resources/design/home/devices.png" />
    <Button
        android:text="Download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btt_main_download" />
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/prb_main_progress"
        android:visibility="gone" />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/img_main_image"
        android:scaleType="centerInside" />
</LinearLayout>

My button download clicked

void mBttDownload_Click(object sender, EventArgs e)
        {
            mPrbProgress.Visibility = ViewStates.Visible;

            var url = new Uri(mEdtURL.Text);

            WebClient webClient = new WebClient();
            webClient.DownloadDataAsync(url);

            webClient.DownloadDataCompleted += webClient_DownloadDataCompleted;
        }

When webClient complete the download

void webClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            mPrbProgress.Visibility = ViewStates.Gone;

            var bytes = e.Result;

            if(bytes != null && bytes.Length > 0)
            {
                var imageBitmap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length);
                mImgImage.SetImageBitmap(imageBitmap);
            }
        }

But e.Result is null. I also set permission Internet in manifest. But nothing change! How can I fix this? Thank you!

Upvotes: 0

Views: 5352

Answers (2)

WickedW
WickedW

Reputation: 2591

I would also recommend the answer to this question for users who want to use a pre-built library to help them with this -

Asynchronously Load Image from URL into ImageView in Xamarin Android

Below taken from the answer by jzferino

Specifically for Droid, could use this - https://components.xamarin.com/view/square.picasso

For X-Platform IOS and Droid -

https://www.nuget.org/packages/Xamarin.FFImageLoading/ (Nuget)

https://github.com/luberda-molinet/FFImageLoading (Source)

FFImageLoading also allows you massive flexibility in how you load your image - i.e. for droid :

https://github.com/luberda-molinet/FFImageLoading/wiki

Upvotes: 1

JamesMontemagno
JamesMontemagno

Reputation: 3792

I would recommend using the UrlImageViewHelper component: UrlImageViewHelper It is pretty great.

Tis is an old school way of doing it: Check in github

Upvotes: 1

Related Questions