java - 通用图像加载器图像缓存不起作用

标签 java android 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);

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

最佳答案

您可以添加以下一个吗?

.resetViewBeforeLoading(false)

您的 DisplayImageOptions 应如下所示:

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

希望这对您有帮助。

关于java - 通用图像加载器图像缓存不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33934541/

相关文章:

android - Google 登录关闭时从上到下出现黑色条纹 - Flutter

android - 使用 Universal Image Loader 显示谷歌静态 map 时出现 FileNotFoundException

android - 如何在 ListView 中实现延迟加载的图像列表

java - 使用 Universal-Image-Downloader 下载图像时出现 SecurityException

java - 使用 Java 和 Spring、消息传递或 RMI 进行分布式计算?

java - 您可以在 Java 中获得的最接近 C# 内部修改的东西是什么?

java - 何时选择 JMS API 而不是 UDP 套接字 API,反之亦然?

Android 开发 - 使用变量打开原始文件或 Assets 文件

android - 在 Android 中使用 Viewflipper 类的图像点击监听器

java - 根据操作结果有条件地更改struts2表单的 'targets'属性