android - ImageLoader - 如果图像已经被缓存,则不要下载图像

标签 android universal-image-loader image-caching

如果图像已经缓存,我不想下载它们。我正在使用 NOSTRA 的 ImageLoader 库。请告诉我是否有办法做到这一点。以下是代码:-

    DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .showStubImage(R.drawable.ic_stub)
                        .showImageForEmptyUri(R.drawable.ic_sample)
                        .resetViewBeforeLoading()
                        .cacheInMemory()
                        .cacheOnDisc()
                        .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                        .bitmapConfig(Bitmap.Config.ARGB_8888)
                        .build();
                imageLoader.displayImage(url2.toString()
                                          ,thumbnail,options, new                                 
                    ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted() {

                       // progressBar.setVisibility(grid.VISIBLE);
                        //  grid.notify();
                    }

                    @Override
                    public void onLoadingFailed(FailReason failReason) {

                       //progressBar.setVisibility(grid.GONE);
                        //  grid.notify();

                    }

                    @Override
                    public void onLoadingComplete(Bitmap bitmap) {


                      //  progressBar.setVisibility(grid.GONE);
                        // grid.notify();
                    }

                    @Override
                    public void onLoadingCancelled() {

                       //  progressBar.setVisibility(grid.GONE);
                        //grid.notify();

                    }
                });

最佳答案

您尚未在 DisplayImageOption 中定义默认缓存选项。

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
    .threadPoolSize(5)
    .threadPriority(Thread.MIN_PRIORITY + 3)
    .denyCacheImageMultipleSizesInMemory()
    // 1MB=1048576 
    .memoryCacheSize(1048576 * 5)
    .discCache(new UnlimitedDiscCache(cacheDir))
    .build();

这里,cacheDir 是一个目录,可能在 SD 卡 上(需要权限“android.permission.WRITE_EXTERNAL_STORAGE”) 或应用程序的缓存目录。

我在我的应用程序中提供了 cacheDirectory

File cacheDir = new File(this.getCacheDir(), "name of directory");
    if (!cacheDir.exists())
        cacheDir.mkdir();

现在,在下载任何图像之前,通过以下代码向 ImageLoader 提供您的配置,

imageLoader.init(config);

关于android - ImageLoader - 如果图像已经被缓存,则不要下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14437933/

相关文章:

android - Android 应用程序包安装在手机上的什么位置

java - Android 游戏开发 - 如何用重复图案填充圆圈?

android - Android L 中的警报对话框按钮问题

android - 图像在图像加载器中被复制

Android Universal Image Loader - 如何正确设置规范?

java - 通用图像加载器 : how to catch id of browsed image?

android - 线圈图像缓存

java - 使用 TextView 制作像素矩阵

ios - Nimbus for iOS 中 NINetworkImageView 的默认缓存行为是什么

android - 如何防止LRU缓存android中的内存不足错误