Universal Image Loader图像缓存无法正常工作

时间:2015-11-26 09:14:34

标签: java android universal-image-loader

我一直在研究使用Universal Image Loader缓存图像的代码。但它似乎并不成功,因为图像仍然从服务器加载,而不是从缓存中加载。这是我用过的代码。我希望有人可以指出我的错误在哪里。谢谢。

    public void setEventImage(String myImageVersion,String myImage){
    ImageEvent imgEvent = (ImageView)findViewById(R.id.image_view);
    String imgUrl = "url of the image"
    String imgEventWidth = "";
    ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .build();
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(displayImageOptions)
            .imageDownloader(new BaseImageDownloader(getApplicationContext(),60*1000,60*1000))
            .diskCache(new UnlimitedDiskCache(getCacheDir()))
            .writeDebugLogs()
            .build();
    imageLoader.init(config);
    imageLoader.loadImage(imgUrl, new SimpleImageLoadingListener() {
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            int width = loadedImage.getWidth();
            int height = loadedImage.getHeight();

            RelativeLayout.LayoutParams imageViewParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            imgEventWidth = imgEvent.getWidth();
            double forivheight = (imgEventWidth * height) / width;
            int finalHeight = (int) Math.round(forivheight);

            imgEvent.setImageBitmap(Bitmap.createScaledBitmap(loadedImage, width, finalHeight, false));
            imgEvent.setLayoutParams(imageViewParams);
        }
    });
}

及以下是调试的日志。有一条线告诉你图像是从网络加载的,而不是缓存。

11-27 08:44:46.772 28037-28037/com.example.me W/ImageLoader: Try to initialize ImageLoader which had already been initialized before. To re-init ImageLoader with new configuration call ImageLoader.destroy() at first.
11-27 08:44:46.782 28037-29220/com.example.me D/ImageLoader: Start display image task [http://mydomain/image/event/1.jpg?version=17_480x854]
11-27 08:44:46.782 28037-29220/com.example.me D/ImageLoader: Load image from network [http://mydomain/image/event/1.jpg?version=17_480x854]
11-27 08:44:46.852 28037-28037/com.example.me I/gralloc.sc8830: gralloc_register_buffer, handle:0x559be660, size:0x190500, fd:60
11-27 08:44:46.952 28037-28037/com.example.me I/gralloc.sc8830: gralloc_register_buffer, handle:0x557b4d38, size:0x190500, fd:71
11-27 08:44:46.982 28037-28037/com.example.me I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x55c40730, size:0x190500, fd:63
11-27 08:44:46.982 28037-28037/com.example.me I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x54c0c7f0, size:0x190500, fd:68
11-27 08:44:46.982 28037-28037/com.example.me I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x50906a58, size:0x190500, fd:48
11-27 08:44:46.982 28037-28037/com.example.me I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x55bc2058, size:0x190500, fd:54
11-27 08:44:48.023 28037-28037/com.example.me I/gralloc.sc8830: gralloc_register_buffer, handle:0x54c0ca30, size:0x190500, fd:48
11-27 08:44:48.053 28037-28037/com.example.me I/gralloc.sc8830: gralloc_register_buffer, handle:0x54c05670, size:0x190500, fd:53
11-27 08:44:48.463 28037-29220/com.example.me D/dalvikvm: GC_FOR_ALLOC freed 195K, 4% free 20235K/20876K, paused 24ms, total 24ms
11-27 08:44:48.463 28037-29220/com.example.me I/dalvikvm-heap: Grow heap (frag case) to 27.666MB for 7990288-byte allocation
11-27 08:44:48.483 28037-28046/com.example.me D/dalvikvm: GC_FOR_ALLOC freed 1K, 3% free 28036K/28680K, paused 25ms, total 25ms
11-27 08:44:49.354 28037-28037/com.example.me D/ImageLoader: Display image in ImageAware (loaded from NETWORK) [http://mydomain/image/event/1.jpg?version=17_480x854]
11-27 08:44:49.384 28037-28037/com.example.me D/dalvikvm: GC_FOR_ALLOC freed 66K, 3% free 27982K/28680K, paused 29ms, total 30ms
11-27 08:44:49.384 28037-28037/com.example.me I/dalvikvm-heap: Grow heap (frag case) to 30.300MB for 2820112-byte allocation
11-27 08:44:49.414 28037-28046/com.example.me D/dalvikvm: GC_FOR_ALLOC freed <1K, 3% free 30736K/31436K, paused 27ms, total 27ms

更新

问题是因为我使用loadedImage Bitmap设置图像:

imgEvent.setImageBitmap(Bitmap.createScaledBitmap(loadedImage, width, finalHeight, false));

我删除了上面的行,并将其替换为以下代码:

imageLoader.displayImage(imgUrl, imgEvent, displayImageOptions);

尽管两个代码都来自通用图像加载器,但似乎我们应该这样做,以便我们可以缓存图像。

1 个答案:

答案 0 :(得分:0)

你可以在下面加一个吗?

.resetViewBeforeLoading(false)

您的DisplayImageOptions应如下所示:

 DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .resetViewBeforeLoading(false)
            .cacheOnDisk(true)
            .build();

希望这会对你有所帮助。