Val
Val

Reputation: 4376

Cache all images via Android Universal Image Loader

I use Android Universal Image Loader library in my project.

I implement offline work but this library starting to load and to cache images only before displaying the view with image. In this case when I turn off internet, this preloaded image will visible for me.

In my case I have array of links and I want download and cache them. Then I will display any of images, not only that was displayed and cached, in offline mode.

Can I force Universal Image Loader to download all images from list of links?

Upvotes: 2

Views: 3153

Answers (2)

aimiliano
aimiliano

Reputation: 1145

I use an Async with this implementation

with also using a Progress Dialog

like this progress=new ProgressDialog(this);
progress.setMessage("Downloading Products Feed");

    progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progress.setIndeterminate(false);
    progress.setCancelable(true);
    progress.setCanceledOnTouchOutside(false);
    progress.setProgress(0);
    progress.show();



 /**
     * Dowload all images
     */
    int mProgressPer=0;
    class imagesloadAsync extends  AsyncTask<String, Void, String> {
        JSONArray all_prods_Available= new JSONArray();
        public imagesloadAsync(JSONArray all_prods) {
            this.all_prods_Available=all_prods;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
        cdata.log("i", "Act_Splash onProgressUpdate", "onProgressUpdate values-> " + values);
    }

    @Override
    protected void onCancelled(String s) {
        super.onCancelled(s);
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @SuppressLint("LongLogTag")
    @Override
    protected String doInBackground(String... params) {
        try {

            //fix new array with images exist only
        final JSONArray All_images= new JSONArray();
        for (int i=0;i<all_prods_Available.length();i++) {
            JSONObject jstemp = all_prods_Available.getJSONObject(i);
                if (!jstemp.isNull("image") && !jstemp.getString("image").equals("")) {
                    All_images.put(jstemp);
                }
            }

            progress.setProgress(All_images.length());
            mProgressPer=0;
            //START DOWNLOADING..

            for (int i=0;i<All_images.length();i++) {
                JSONObject jstemp = All_images.getJSONObject(i);
                final int finalI = i;
                String imageURL=jstemp.getString("image");
                Log.i("Act_Splash trying on image:","image treying->"+imageURL);

                imageloader.loadImage(imageURL, new SimpleImageLoadingListener() {

                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        mProgressPer++;
                        progress.setProgress(mProgressPer);

                        if (mProgressPer == All_images.length()){
                            progress.dismiss();
                            startActivity(new Intent(Act_Splash.this, Act_main.class));
                            finish();
                        }

                        Log.i("Act_Splash imageloader onLoadingComplete", "onLoadingComplete for imageUri->"+imageUri+ " and");
                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                        mProgressPer++;

                        if (mProgressPer == All_images.length()){
                            progress.dismiss();
                            startActivity(new Intent(Act_Splash.this, Act_main.class));
                            finish();
                        }

                        progress.setProgress(mProgressPer);

                        Log.w("Act_Splash imageloader onLoadingFailed", "onLoadingFailed for imageUri->" + imageUri);
                    }
                });




            }




        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Upvotes: 0

atmchile
atmchile

Reputation: 21

For only cache images, call this:

//Cache image...
loader.loadImage(imagenUrl, new SimpleImageLoadingListener());

Upvotes: 2

Related Questions