How to use Universal Image Loader

前端 未结 5 2094

I have a requirement where I need to load thumbnails and a Text in ListView which gets set by the custom Adapter. The Thumbnails shoul

相关标签:
5条回答
  • 2020-12-09 19:29

    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);
    
    0 讨论(0)
  • 2020-12-09 19:36

    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

    0 讨论(0)
  • 2020-12-09 19:37

    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

    0 讨论(0)
  • 2020-12-09 19:41
    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");
                    }
                }
            }
    }
    
    0 讨论(0)
  • 2020-12-09 19:49

    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() {
    
    0 讨论(0)
提交回复
热议问题