【问题标题】:How to enable cache for bitmap using SimpleImageLoadingListener in Universal Image Loader android如何在 Universal Image Loader android 中使用 SimpleImageLoadingListener 启用位图缓存
【发布时间】:2016-12-21 05:22:40
【问题描述】:

您好,我使用下面的代码在小部件列表视图项上加载图像。

Map<Integer, Boolean> flags = Collections.synchronizedMap(new HashMap<Integer, Boolean>());
    Bitmap mBitmap;
    Handler handler = new Handler(Looper.getMainLooper());


 flags.put(position, false);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    ImageLoader imageLoader = ImageLoader.getInstance();
                    DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
                            .cacheOnDisc(true).resetViewBeforeLoading(true)
                            .build();
                    imageLoader.loadImage(item.mTooteet.getThumbUrl1() + "&userToken=" + userPreference.getUserToken(), options,
                            new SimpleImageLoadingListener() {
                                @Override
                                public void onLoadingStarted(String imageUri, View view) {
                                    Log.d(TAG,"onLoadingStarted  "+position);
                                }

                                @Override
                                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                                    Log.d(TAG,"onLoadingFailed  "+position);
                                    flags.put(position, true);
                                }

                                @Override
                                public void onLoadingCancelled(String imageUri, View view) {
                                    Log.d(TAG,"onLoadingCancelled  "+position);
                                    flags.put(position, true);
                                }

                                @Override
                                public void onLoadingComplete(String arg0, View arg1, Bitmap bitmap) {
                                    Log.d(TAG,"onLoadingComplete  "+position);
                                    mBitmap = bitmap;
                                    flags.put(position, true);
                                }
                            });
                }
            });

            while (!flags.get(position)) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            flags.put(position, false);
            if (mBitmap != null) {
                Log.d(TAG,"onLoadingComplete mBitmap not null "+position);
                remoteView.setImageViewBitmap(R.id.feed_image, mBitmap);
            } else {
                Log.d(TAG,"onLoadingComplete mBitmap null "+position);
                remoteView.setImageViewResource(R.id.feed_image, R.drawable.app_icon);
            }
            mBitmap = null;

在我的应用程序文件中,我使用了以下代码,

 // UNIVERSAL IMAGE LOADER SETUP
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheOnDisc(true).cacheInMemory(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .displayer(new FadeInBitmapDisplayer(300)).build();

        ImageLoaderConfiguration uilConfig = new ImageLoaderConfiguration.Builder(
                getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .memoryCache(new WeakMemoryCache())
                .discCacheSize(100 * 1024 * 1024).build();

        ImageLoader.getInstance().init(uilConfig);
        // END - UNIVERSAL IMAGE LOADER SETUP

但是这段代码每次都会从 url 下载图片。我不想每次滚动我的应用小部件列表视图时都重新加载它。第一次下载后,它应该保存在缓存中。你能建议我这样做吗?

【问题讨论】:

  • 这不是您问题的解决方案,而是建议您为什么不能使用glide。在我的情况下它更好。我也希望你?
  • @Raghavendra 嗨,我正在使用小部件,我们可以像这样在图像视图上设置图像 remoteView.setImageViewBitmap(R.id.feed_image, mBitmap);其中 R.id.feed_image 是我的图像视图 ID。我在小部件中使用 glide 库时遇到了麻烦。你知道如何在这样的小部件中使用滑翔吗?

标签: android bitmap widget android-widget


【解决方案1】:

从您的代码中,您每次都在创建 DisplayImageOptions。我认为这是一次制作 DisplayImageOptions 的原因(在您的 Runnable 对象之外) 像这样使用:

ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
                        .cacheOnDisc(true).resetViewBeforeLoading(true)
                        .build()

然后在你的其他代码里面运行

   handler.post(new Runnable() {
            @Override
            public void run() {
                 imageLoader.loadImage(..........)///same code you written

}

});

请告诉我结果。

【讨论】:

  • 我已经按照你说的尝试过了。但它再次下载。
  • 通用图像加载器首先使用 imgUrl 检查您的缓存是否存在此 imgUrl 的任何缓存。如果找到,则从缓存中加载它,否则从服务器下载并缓存它。请检查每次加载后在 imageView 中显示图像需要多少时间。如果从第一次加载到下一次加载的时间不同,则应从缓存中加载图像。
  • @MdFazlaRabbiOpu 这需要几秒钟。有时需要超过一分钟。
  • @Sangeetha 我的意思是检查第一次和第二次加载的相同图像查看时间。查看时差。
  • 第一次加载需要 2 秒,第二次加载需要 4 秒,因为我移动到包含 50 个项目的列表末尾。有时,在向下滚动时,首次加载本身需要比这个时间更长的时间。
猜你喜欢
  • 1970-01-01
  • 2012-12-13
  • 2012-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-24
  • 2017-09-08
  • 1970-01-01
相关资源
最近更新 更多