4

I am using volley library to download network responses. I know volley download images when it is needed only. But what I want to achieve is to download all the images at a time and store in my cache memory.

So basically I want to download all the images at once and store in cache so if there will no interenet also user can see all the images.

I am able to store images in cache using my own LruBitmapCache class.And to download images I am using following method

public void downloadImage(NetworkImageView view, MyApplication application, String imageUrl, Context ctx) {
    NetworkImageView image = view;
    ImageLoader loader = new ImageLoader(application.getRequestQueue(), new LruBitmapCache(
            LruBitmapCache.getCacheSize(ctx)));
    image.setImageUrl(imageUrl, loader);
}

This is working fine in downloading the images and storing in cache. But it is only downloading images those are in my main screen.

To download all the images I tried following method

public void downloadCoverImages(){

  int i = 0;

  for(ImageData data: GetData._instance.getImageList()){

    NetworkImageView iv = new NetworkImageView(BaseActivity.this);
    iv.setLayoutParams(new LinearLayout.LayoutParams(300,150));

    GetData._instance.downloadImage(iv, (MyApplication) getApplicationContext(),
            data.getImages().getSmallImage(),BaseActivity.this);

    //Log.e("Download",++i+" "+data.getImages().getSmallImage());
  }

}

But it's not to download an image a proper ImageView is required I guess and that also needs to in main screen. So in this case what should I do?

My singleton class as follows:

public enum GetData {
    _instance;
    private List<Sections> sectionsList = new ArrayList<Sections>();
    private List<DealDetails> dealDetailsList = new ArrayList<DealDetails>();
    private DealDetails dealDetails;

    public List<Sections> getSectionsList() {
        return sectionsList;
    }

    public void setSectionsList(List<Sections> sectionsList) {
        this.sectionsList = sectionsList;
    }

    public List<DealDetails> getDealDetailsList() {
        return dealDetailsList;
    }

    public void setDealList(List<DealDetails> dealDetailsList) {
        this.dealDetailsList = dealDetailsList;
    }

    public DealDetails getDealDetails() {
        return dealDetails;
    }

    public void setDealDetails(DealDetails dealDetails) {
        this.dealDetails = dealDetails;
    }


    public void downloadImage(NetworkImageView view, MyApplication application, String imageUrl, Context ctx) {
        NetworkImageView image = view;
        ImageLoader loader = new ImageLoader(application.getRequestQueue(), new LruBitmapCache(
                LruBitmapCache.getCacheSize(ctx)));
        image.setImageUrl(imageUrl, loader);
    }
}

Is it possible if I download image without using volley and store in my LRUCache and show it using volley? Any good tutorial for that? Thnaks.

Kunu
  • 3,825
  • 3
  • 27
  • 57

1 Answers1

1

you can also get bitmap of remote image using volley imageloader.. in this way you don't need to have imageview and all

imageLoader.get(data.getImages().getSmallImage(), new ImageLoader.ImageListener() {
        @Override
        public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {

        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

get the imageloader instance from volley singleton class. in your case you don't need to handle the imagelistener callbacks ..

Dinesh Kannan
  • 1,195
  • 12
  • 28
  • Yeah but my concern is I am storing images in cache. Right now it is checking for imageurl in cache. Can you please suggest how can I implement my own cache in this method? – Kunu Apr 30 '15 at 07:18
  • I have updated it. It's just a normal singleton class. – Kunu Apr 30 '15 at 08:29
  • @kunu why do you need your own cache?.. to download and cache images in single time ?.. – Dinesh Kannan Apr 30 '15 at 08:32
  • because what I understood is Volley does not provide image caching. So you have to implement by yourself only. – Kunu Apr 30 '15 at 08:39
  • ImageLoader loader = new ImageLoader(application.getRequestQueue(), new LruBitmapCache( LruBitmapCache.getCacheSize(ctx))); in this line LruBitmapCache is used for caching only.. bitmap memory caching support is provided by volley.. – Dinesh Kannan Apr 30 '15 at 08:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76612/discussion-between-user1992200-and-kunu). – Dinesh Kannan Apr 30 '15 at 08:43
  • Did you guys found a solution to this problem? I too would like to download all the images in one call.... – Vinchenzo Jul 03 '15 at 11:21