Search code examples
androidandroid-volleynetworkimageview

Android Volley: Perform task after imageload


I am using Volley Library to download an image. I want to change the text of a TextView after the image is loaded. Like, before the text is loading. and after the image is loaded it should change to loaded.

NetworkImageView image = (NetworkImageView)findViewById(R.id.imageView1);
image.setDefaultImageResId(R.drawable.def);
image.setImageUrl(url, ImageUtil.getImageLoader(getApplicationContext()));

How can i listen or call a function when the image is completely loaded?


Solution

  • You can try as in code snippet below..

    ImageView image = ImageView.class.cast(layout.findViewById(R.id...));
        String url = "..."; // URL of the image
    
        mImageLoader.get(url, new ImageListener() {
    
                    public void onErrorResponse(VolleyError arg0) {
                        image.setImageResource(R.drawable.icon_error); // set an error image if the download fails
                    }
    
                    public void onResponse(ImageContainer response, boolean arg1) {
                        if (response.getBitmap() != null) {
                            image.startAnimation(AnimationUtils.loadAnimation(container.getContext(), android.R.anim.fade_in));
                            image.setImageBitmap(response.getBitmap());
                        } else
                            image.setImageResource(R.drawable.icon_loading); // set the loading image while the download is in progress
                    }
                });
    

    you can create your mImageLoader instance as below to implement cache

    mRequestQueue = Volley.newRequestQueue(context);
    mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
        private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
        public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
        }
        public Bitmap getBitmap(String url) {
            return mCache.get(url);
        }
    });'