How to use Universal Image Loader
Asked Answered
S

5

12

I have a requirement where I need to load thumbnails and a Text in ListView which gets set by the custom Adapter. The Thumbnails should be stored in a cache memory, for that I am using the Universal Image Loader however I am pretty much confused in the implementation of it and how to use it as per my requirement in to load the images in ListView from URL. Please suggest me some ways for it with good implementation.

Spectator answered 13/12, 2012 at 6:20 Comment(0)
S
15

Write below code line into your adapter's getView() method, here imageUrls[position] is array of Image Urls and holder.image is imageview.

imageLoader.displayImage(imageUrls[position], holder.image, null);

And write below code line into your adapter constructor.

ImageLoader imageLoader=new  ImageLoader(activity.getApplicationContext());

it will solve your problem, And if you have any query regarding that then tell me.

And see below link for complete source code of Universal Image Loader Example.

Android - Universal Image Loader

Shyster answered 13/12, 2012 at 6:40 Comment(3)
Hello Dipak I have gone through as you suggested and Got these problems please see and help me on the question posted by me only..#13857315Spectator
as well as this question - #13855913Spectator
@Dipak Keshariya are these the only steps?Twice
G
8

In your adapter's oncreate() define

 ImageLoader imageLoader=new  ImageLoader(activity.getApplicationContext());

and use it in the getView() method:

imageLoader.DisplayImage(//your image url, //your imageView);
Gopher answered 13/12, 2012 at 6:28 Comment(0)
G
8

I will suggest you using AQuery - (Android-Query) - a super simple UI manipulation framework for Android.

AQuery comes as a library, which you need to include in your build path.

In AQuery, you can download, display (with effects) and cache the image (both in memory and disk) with following lines:

AQuery aq = new AQuery(yourActivity.this);
boolean memCache = true;
boolean fileCache = true;
aq.id(R.id.image1).image("http://www.example.com/image.jpg", memCache, fileCache);

AQuery will handle all the data downloading processes, and will display images on your ImageView you've given. Once you load the image, it will be cached in the memory (or disk) according to the boolean parameters memCache and fileCache. The next time, it will load the image from the memory cache or file cache.

For more information and examples, you should visit the AQuery Project at http://code.google.com/p/android-query/

More code on Image Loading - http://code.google.com/p/android-query/wiki/ImageLoading

Godfree answered 13/12, 2012 at 6:34 Comment(3)
OK I will try but will it give any kind of Out of memory erros because the images on server are of very high resolution.Spectator
@NitinBathija If the image is too large, then there should be some implementations to reduce the size and deliver it to the phone. Delivering a 15 megapixels image to a 320x240 px screen is not a good idea - rather resize it and deliver - you will save both data plan and disk space!Godfree
I also used android query in my previous projects but can't figure out a way to use it in this case https://mcmap.net/q/910269/-two-async-calls-in-getview-am-i-doing-this-right/1503130Timtima
P
1

This will Help you to load imageurl using universal imageloader it will give status for imageurl is start to Loading , Completed or failed and request is cancel status also providing.I hope it may help you..

 public void ImageLoaderListener(String url){

        imageLoader.loadImage(url,new ImageLoadingListener(){
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                Log.e("tag", "onLoadingStarted");
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                Log.e("tag", "onLoadingFailed");
            }
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                Log.e("tag", "onLoadingComplete");
                imageView.setImageBitmap(loadedImage);
            }
            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                Log.e("tag", "onLoadingCancelled");
            }
        });
    }

if you like to cache the image add this below functions... But to initiate on create because its very highly ..

 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
            .build();

    imageLoader = ImageLoader.getInstance();

    if (!imageLoader.isInited()) {
        imageLoader.init(config);
    }
    defaultOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .build();

Then you will add that ImageLoadingListener() function...

public void ImageLoaderListener(String url){
  imageLoader.loadImage(url, defaultOptions, new ImageLoadingListener() {
Paoting answered 20/10, 2016 at 12:39 Comment(0)
L
0
public class CategoryModel 
{
        String CatId;
        String CatName;
        String Image;
        String Price;
        Bitmap ImgSrc;
        CategoryAdapter Ma;
        public CategoryModel()
        {
        }
        public CategoryModel(String catid,String catname,String image,String price)
        {
            this.CatId = catid;
            this.CatName = catname;
            this.Image = image;
            this.Price = price;
        }
        public CategoryModel(String catname,String image,String price)
        {
            this.CatName = catname;
            this.Image = image;
            this.Price = price;
        }
        public void setCatId(String catid)
        {
            this.CatId =catid;
        }
        public String getCatId()
        {
            return this.CatId;
        }
        public void setCatName(String catname)
        {
            this.CatName=catname;
        }
        public String getCatName()
        {
            return this.CatName;
        }
        public void setImage(String image)
        {
            this.Image=image;
        }
        public String getImage()
        {
            return this.Image;
        }
        public void setPrice(String price)
        {
            this.Price=price;
        }
        public String getPrice()
        {
            return this.Price;
        }
        public Bitmap getImagesrc()
        {
            return this.ImgSrc;
        }
        public void setAdapter(CategoryAdapter adf)
        {
            this.Ma = adf;
        }
        public CategoryAdapter getAdapter()
        {
            return this.Ma;
        }

        public void LoadImage(CategoryAdapter adap)
        {
            this.Ma = adap;
            if(Image != null && !Image.equals(""))
            {
                new ImageLoader().execute(new String[]{ Image });
            }
        }
        public static Bitmap getBitmapUrl(String src)
        {
            try
            {
                URL url = new URL(src);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap mybitmap = BitmapFactory.decodeStream(input);
                connection.disconnect();
                return mybitmap;
            }
            catch (Exception e) {
                return null;
            }
        }
        private class ImageLoader extends AsyncTask<String, String, Bitmap>
        {
            @Override
            protected void onPreExecute()
            {
            }
            @Override
            protected Bitmap doInBackground(String... params) {
                try
                {
                    Bitmap b = getBitmapUrl(params[0]);
                    return b;
                }
                catch (Exception e)
                {
                    Log.e("Excep", e.toString());
                    return null;
                }
            }
            @Override
            protected void onPostExecute(Bitmap res)
            {
                if(res!=null)
                {
                    ImgSrc = res;
                    if(Ma!=null)
                    {
                        Ma.notifyDataSetChanged();
                    }
                }
                else
                {
                    Log.e("Error", "Image not Loading");
                }
            }
        }
}
Leitmotiv answered 13/5, 2017 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.