通过Android Universal Image Loader缓存所有图像

时间:2013-12-26 14:50:23

标签: android image caching universal-image-loader image-loading

我在项目中使用Android Universal Image Loader库。

我实现了离线工作,但是这个库开始加载并仅在显示带有图像的视图之前缓存图像。在这种情况下,当我关闭互联网时,这个预加载的图像对我来说是可见的。

在我的情况下,我有一系列链接,我想下​​载并缓存它们。然后,我将在离线模式下显示任何图像,而不仅仅是显示和缓存的图像。

我可以强制Universal Image Loader从链接列表中下载所有图片吗?

2 个答案:

答案 0 :(得分:2)

仅限缓存图片,请调用:

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

答案 1 :(得分:0)

我在此实现中使用Async

同时使用进度对话

像这样  progress = new ProgressDialog(this);
        progress.setMessage(“下载产品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;
    }
}