Search code examples
androidlazy-loadinguniversal-image-loader

Unable to set image view background image using universal image loader?


I am trying to set background for the image view. But not got luck to set background using universal image loader.

I am using following code.

DisplayImageOptions options = new DisplayImageOptions.Builder()
                                    .showStubImage(R.drawable.mo_no_image)
                                    .showImageForEmptyUri(R.drawable.mo_no_image)
                                    .cacheInMemory().cacheOnDisc()
                                    .displayer(new RoundedBitmapDisplayer(0))
                                    .imageScaleType(ImageScaleType.EXACTLY).build();
ImageLoader.getInstance().displayImage("url_to_load_image", imgView, options);

By using above code I am able to set image resource which check aspect ratio and using that modifying image size. I want to set downloaded image to the background of image view.


Solution

  • I can recommend a different way that works like a charm: Android Query.

    You can download that jar file from http://code.google.com/p/android-query/downloads/list

    AQuery androidAQuery=new AQuery(this);
    

    As an example:

    androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);
    

    It's very fast and accurate, and using this you can find many more features like Animation when loading; getting a bitmap, if needed; etc.

    In your case you need to fetch Bitmap, so for it, use below code

    androidAQuery.ajax(YOUR IMAGE URL,Bitmap.class,0,new AjaxCallback<Bitmap>(){
                            @Override
                            public void callback(String url, Bitmap object, AjaxStatus status) {
                                super.callback(url, object, status);
    
                                //You will get Bitmap from object.
                            }
    
    });
    

    Use this bitmap to set your background resource of ImageView using method.