Search code examples
c#androidxamarinuniversal-image-loader

xamarin universal image loader without putting it in view


i'm trying to load an image into a variable using universal image loader in xamarin. however it always returns null. i've tried several methods but none seem to do the job. this is my code. the only one that works in the none universal image loader one.

private async void GetImages(CardsAdapter adapter, List<Card> cards)
        {
            WebClient client = new WebClient();

            foreach (var card in cards)
            {
                //var data = await client.DownloadDataTaskAsync(card.imageUrl);
                //card.image = new BitmapDrawable(BitmapFactory.DecodeByteArray(data, 0, data.Length));
                //card.image = ImageService.AsBitmapDrawableAsync();
                //ImageLoader imageLoader = ImageLoader.Instance;
                //Bitmap bm = imageLoader.LoadImageSync(card.imageUrl);
                var bm = await LoadImage(card);
                card.image = new BitmapDrawable(bm);
                adapter.NotifyDataSetChanged();
            }
        }

        private async Task<Bitmap> LoadImage(Card card)
        {
            ImageLoader imageLoader = ImageLoader.Instance;
            Bitmap bm = imageLoader.LoadImageSync(card.imageUrl);
            //imageLoader.LoadImage(card.imageUrl, new SimpleImageLoadingListener());
            //Bitmap bm = null;
            //imageLoader.LoadImage(
            //    card.imageUrl,
            //    new ImageLoadingListener(
            //        loadingComplete: (imageUri, view, loadedImage) => {
            //            // Do whatever you want with Bitmap
            //            bm = loadedImage;
            //        }));
            return bm;
        }

thanks for your help!

regards,

Bjorn


Solution

  • After several tests of your code, your code ran out the android.os.NetworkOnMainThreadException exception or ImageLoader must be init with configuration before using error by my side. The first exception didn't make the program stop but it is thrown in the log. Maybe it's the reason why your Bitmap all get null.

    You can try this code:

    public class MainActivity : Activity, IImageLoadingListener
    {
        //Your ListView and Adapter
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            // Set your ListView and Adapter here
    
            ImageLoader imageloader = ImageLoader.Instance;
            imageloader.Init(ImageLoaderConfiguration.CreateDefault(this));
    
            foreach (var card in cards)
            {          
                imageloader.LoadImage(card.imageUrl, this);
            }            
        }
    
        public void OnLoadingCancelled(string p0, View p1)
        {
        }
    
        public void OnLoadingComplete(string p0, View p1, Bitmap p2)
        {
            foreach (var card in cards)
            {
               if (card.imageUrl == p0)
               {
                  card.image = p2;
                  adapter.NotifyDataSetChanged();
               }               
            }
        }
    
        public void OnLoadingFailed(string p0, View p1, FailReason p2)
        {
        }
    
        public void OnLoadingStarted(string p0, View p1)
        {
        }
    }
    

    This code works by me, if still doesn't work, please ensure you've enabled the Internet capability and your image urls are correct.