Search code examples
android-volleynetworkimageviewsubsampling

Load image into custom Imageview rather than NetworkImageView while using volley


I need to load the image in Pinview rather than native NetworkImageView while using volley to load images from server.

I went through lot of articles and I found nothing. Is there any way to do so?


Solution

  • Well I don't know if anyone will find it useful but just in case.

    My requirement was that I needed to load an image to my custom imageview for which I needed the bitmap.

    I used a Singleton class.

    public class MyVolleySingleton {
    
    private static MyVolleySingleton mInstance;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;
    
    private MyVolleySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();
        mImageLoader = getImageLoaderRequest();
    }
    private ImageLoader getImageLoaderRequest() {
        return new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap>
                            cache = new LruCache<String, Bitmap>(20);
                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }
                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                }
        );
    }
    
    public static synchronized MyVolleySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MyVolleySingleton(context);
        }
        return mInstance;
    }
    
    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }
    
    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }
    public ImageLoader getImageLoader() {
        return mImageLoader;
    }
    
    }
    

    Now in the main activity, I used the function in which I passed the url of the image which has to be loaded.

        private void loadImage(String plan_url) {
            String url = plan_url;
            if (url.equals("")) {
                Toast.makeText(this, "<Error Message>", Toast.LENGTH_LONG).show();
                return;
            }
    
            // Retrieves an image specified by the URL, displays it in the UI.
            ImageRequest request = new ImageRequest(url,
                    new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap bitmap) {
                            imageView.setImage(ImageSource.bitmap(bitmap));
    
                        }
                    }, 0, 0, null,
                    new Response.ErrorListener() {
                        public void onErrorResponse(VolleyError error) {
                            imageView.setImage(ImageSource.resource(R.drawable.demo1));
                        }
                    });
    // Access the RequestQueue through your singleton class.
            MyVolleySingelton.getInstance(this).addToRequestQueue(request);
        }
    

    Volley developer docs