Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通用映像加载程序映像缓存不工作_Java_Android_Universal Image Loader - Fatal编程技术网

Java 通用映像加载程序映像缓存不工作

Java 通用映像加载程序映像缓存不工作,java,android,universal-image-loader,Java,Android,Universal Image Loader,我一直在编写关于使用通用图像加载器缓存图像的代码。但它似乎并不成功,因为映像仍在从服务器加载,而不是从缓存加载。这是我用过的代码。我希望有人能指出我的错误所在。多谢各位 public void setEventImage(String myImageVersion,String myImage){ ImageEvent imgEvent = (ImageView)findViewById(R.id.image_view); String imgUrl = "url of

我一直在编写关于使用通用图像加载器缓存图像的代码。但它似乎并不成功,因为映像仍在从服务器加载,而不是从缓存加载。这是我用过的代码。我希望有人能指出我的错误所在。多谢各位

    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
我删除了上面的一行,并将其替换为下面的代码:

imageLoader.displayImage(imgUrl, imgEvent, displayImageOptions);

虽然这两个代码都来自universal image loader,但似乎我们应该这样做,以便缓存图像。

您能在下面添加一个吗

.resetViewBeforeLoading(false)
您的DisplayImageOptions应如下所示:

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

希望这对您有所帮助。

您能在下面添加一个吗

.resetViewBeforeLoading(false)
您的DisplayImageOptions应如下所示:

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

希望这会对您有所帮助。

您认为“它似乎不成功”的确切含义是什么?谢谢。请描述您的问题。作为诊断的唯一输入,“它似乎不成功”没有帮助。对不起,我的意思是图像仍然需要太多的时间才能加载。看起来图像仍然是从服务器加载的,而不是从SD卡加载的。谢谢。您需要将日志级别设置为在
Universal Image Loader
上调试,然后您将看到加载的每个图像URL,并且日志会显示它从何处加载。我看到了从(
Network
Disk Cache
Mem Cache
)加载的日志,因此在这种情况下,您将发现发生了什么,因为
slow
花费的时间太长
不应该意味着它不工作……那么“它似乎不成功”到底是什么意思呢?谢谢。请描述您的问题。作为诊断的唯一输入,“它似乎不成功”没有帮助。对不起,我的意思是图像仍然需要太多的时间才能加载。看起来图像仍然是从服务器加载的,而不是从SD卡加载的。谢谢。您需要将日志级别设置为在
Universal Image Loader
上调试,然后您将看到加载的每个图像URL,并且日志会显示它从何处加载。我看到了从(
Network
Disk Cache
Mem Cache
)加载的日志,因此在这种情况下,您将发现发生了什么,因为
slow
花费的时间太长
并不意味着它不起作用……谢谢,实际上问题是因为我加载图像时使用了loadImage()方法提供的“LoadeImage”位图,而不是我在上面更新的帖子中提到的displayImage()方法。谢谢,实际上,问题是因为我加载图像时使用了loadImage()方法提供的“loadeImage”位图,而不是我在上面更新的帖子中提到的displayImage()方法。